Node. js obtains data from the database and node. js database.

Source: Internet
Author: User

Node. js obtains data from the database and node. js database.

In this article, we need to use node. js as a small function to obtain data from Sqlserver and display the data on the page. Here we will share with you:
App. js:

Var sqlbll = require ('. /routes/sqlbll ');... app. get ('/sqlbll/: ver', function (req, res) {var versionId = req. params. ver; // sqlbll. getData is the method for obtaining data in the route. // The second parameter is written as the callback function. Sqlbll. getData (versionId, function (data) {res. json (data); // send json data}); // The route file ==== sqlbll. js ====... exports. getData = function (ver, back ){//... here, the 1000-word method for obtaining SQL data is omitted. back (jsondata )}

Next, we can directly use the get Method for requests on the page. Of course, the post method is similar.

Var express = require ('express '); var app = express (); app. configure (function () {// by default, Express does not know how to handle the Request body. Therefore, we need to add the bodyParser middleware, used to analyze // application/x-www-form-urlencoded and application/json // Request body, and store the variables in req. body. We can "use" middleware as follows [this ensures that POST can get the value of the Request Parameter]: app. use (express. bodyParser () ;}); // process the POST request // The name and email are the parameter names in the POST request domain app. post ('/hello', function (req, res ){.......});

In addition, I found that when the textarea control changes its text and html attributes, the original value remains unchanged,
This feature has been incredible for a while.

The following is another example,Nodejs obtains information about a specific data table., The specific content is as follows:

Var mysql = require ('mysql'); var conn = mysql. createConnection ({host: 'localhost', user: 'root', password: '000000', database: 'mysql', port: 123456 }); var tempArr = new Array (); conn. connect (); conn. query ("show tables", function (err, results) {if (err) {throw err;} if (results) {for (var I = 0; I <results. length; I ++) {tempArr [I] = results [I]; var a = tempArr [I]; console. log (typeof (results [I]); console. log (a); var temp = ""; temp + =. tables_in_mysql; console. log ("temp" + I + "" + temp); conn. query ("select * from" + "" + temp, function selectCb (err, result, fields) {if (err) {throw err;} console. log ("data table:" + results [I]); console. log (fields );});}}})

The above is all the content of this article, hoping to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.