Home > front end >  Ask why want to get twice to res. Send the correct results
Ask why want to get twice to res. Send the correct results

Time:03-15

Var express=the require (' express ');//introduce express module
Var mysql=the require (' mysql ');//into mysql module

Var fs=the require (" fs ");
Var URL=the require (' URL ');
Var app=express ();//create express an instance of the
Var bodyParser=the require (' body parser - ');
//const bodyParser=the require (' body parser - ');
App. Use (bodyParser. Json ());
App. Use (bodyParser. Urlencoded ({extended: false}));
Var connection=mysql. The createConnection ({//create a mysql instance
Host: '192.168.48.128'
Port: '3306',
User: 'root',
Password: 'password',
The database: 'mydb'
});
Connection. The connect ();
//here is solving the problem of cross-domain request
App. All (' * ', function (the req, res, next) {
Res. The header (" Access - Control - Allow - Origin ", "*");
Res. The header (" Access - Control - Allow - Headers ", "X - Requested - With");
Res. The header (" Access - Control - Allow - the Methods ", "PUT, POST, GET, DELETE, the OPTIONS");
Res. The header (" X - Powered By ", '3.2.1);
Res. The header (" the content-type ", "application/json; charset=utf-8");
next();
});
Var SQL='SELECT * FROM goods';
Var text="";
Connection. The query (SQL, function (err, result) {
If (err) {
The console. The log (' [the SELECT ERROR] : 'err. Message);
}
Text=JSON. Stringify (result);
The console. The log (result);//database query results returned to the result of

});
//connection. Destroy ();
Var router=express. The router ();
The router. The get ('/', function (the req, res, next) {

Res. The send (" welcome ");
//res. SendStatus (200);
});
Var temp="";

The router. The get ('/read ', function (the req, res, next) {
Var params=URL. Parse (the req. URL, true). The query;
//the connection. The connect ();
Connection. The query (' select * from goods where id='+ params. Id +'; ', function (err, result, next) {

If (err) {
The console. The log (' [the SELECT ERROR] : 'err. Message);
} temp=JSON. Stringify (result);
The console. The log (temp);//database query results returned to the result of
});
Res. The send (temp);
//res. SendStatus (200);
//connection. Destroy ();
});
The router. The get ('/h 'function (the req, res) {
Res. The send (" h ");
});
Router. Get ('/G ', function (the req, res) {
Res. The send (" G ");
});
App. Use ('/', the router).
//the connection. The end ();
App. Listen (1337, function () {
//listen port 3000
The console. The log (' Server running at port 1337 ');
});


  • Related