This paper introduces the

method to build a simple query server using express, for everyone to share, as follows:

technology stack

used in express, mysql.

 service --node_modules project: --app.js --query.js

app.js support service calls to the request function, the use of body-parser.

app.js.

query.js link code the database and query the database as follows:

 var Express ('express') var = require; query = require ('./query') var bodyParser ('body-parser') var = require; cookieParser = require; app = ('cookie-parser') var (express); app.use (bodyParser.urlencoded (extended: {false})) / / the returned object is a the key, when extended is false, the key to the value for In the form of'String'or'Array', for true, it can be for any data type. App.use ((bodyParser.json)) / / cross domain support app.all ('*', function (req, res, next) {res.header ("Access-Control-Allow-Origin", "*"); res.header ('Access-Control-Allow-Methods','PUT, GET, POST, DELETE, OPTIONS'); res.header ("Access-Control-Allow-Headers", "X-Requested-With" ('Access-Control-Allow-Headers', res.header);'Content-Type' (next));});; / / log app.post ('/login', => (req, RES) {VaR; opts = req.body; query ("SELECT *FROM `v_users` WHERE userAcount =?", opts.userName.Then (result) (=> response) {var = result[0]; if (opts.password! = response.u_password) {return res.send ({errorCode:'404', errorMsg:'}} password error') to simulate loginToken var / / loginToken = response. UserAcount + Math.random (*Math.pow) (10,16) res.send (loginToken:loginToken) {}}}) = app.listen (VaR) server 3000 (=>); console.log ('success') {}) as follows: 

 

query.js code (function (VaR) {MySQL = require ('mysql'); / / var session = require ('cookie-session'); VAR = query (SQL, key) => mysql.createConnection (connection = {var {host:'localhost', user:'root', password:'root123', database:'m_users'connection.connect (VaR)}); promise = new (Promise (resolve, => reject) {connection.query (SQL, [key], function (error, results, fields) {if (error) {reject (error)}else{resolve (results);}}); Co Nnection.end ();}; return promise;} module.exports = query;}) ()

practice summary:

1.express's entry-level usage, and the usage of body-parser and MySQL plug-ins.

2. debugger Inspector to try to achieve the debugging node program, by the, way individuals are more accustomed to using gulp to debug.

3. client using post access interface to distinguish between Content-Type:

Content-Type:application/json; charset= all the UTF-8 parameter in requestPayload

Content-Type: or the application/x-www-form-urlencoded parameter is not set in the above Form Data

. I hope to help everyone to learn, I hope you will support a script.