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.