Construct a simple query server using Express

This article describes how to build a simple query server using Express, share it to everyone, specifically:

Use the technology stack with Express, MySQL.

item Structure:

Service – Node_Modules – app.js – query.js
   
App.js support Call service, use Body-Parser to process the request.

Query.js implements the link database and query the functionality of the database.

App.js code is as follows:

VAR Express = Require (‘express’); var query = required (‘./ query’) var bodyparser = Require (‘body-parser’); var cookieparser = request (‘cookie-parser’); var app = expression (); app.use ({extended: false}) // The object returned is a key value, when the extended is false, the key value The value of the in the form of ‘string’ or ‘array’ is in the form of TRUE, it can be 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 -Headers”, “X-Requested-with”); Res. Header (‘ Access-Control-Allow -Headers ‘,’ Content-Type ‘); Next ();}); // Login app.post (‘ / login “, (REQ, RES) => {var Opts = Req.body; query (” SELECT * From `v_users` where useracount =?”, Opts.username) .then ((result) => {var response = result [0]; if (OPTS.Password! == Response.u_password) {Return Res.Send ({ ErrorCode: ‘404’, ErrorMsg: ‘Login Password Errors’}}} // Simulation Generate Logintoken Var Logintoken = Response.userCount + Math.random () * Math.Pow (10,16) Res.send ({Logintoken: Logintoken })})}) VAR server = app.listen (3000, () => {Console.log (‘success’)})
   query.js code is as follows : 
(Function () {Var mysql = Require (‘mysql’); // var session = Require (‘cookie-session’); var query = (sql, key) => {var connect = mysql.createconnection ({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);}}); connection.end ();}; return promise;} Module.Exports = query;})

Practical summary:
  1.Express entry-level usage, and usage to the Body-Parser and MySQL plug-ins.  
2. Try to use Inspector debug Node programs to implement Debugger, by the Way individual is more accustomed to use Gulp to debug.

3. Client uses POST to adjust the interface ofIt is time to distinguish between Content-Type:

Content-Type: Application / JSON; Charset = UTF-8 parameter is placed in RequestPayLoad

Content-Type: Not set Or Application / X-WWW-FORM-URLENCODED parameter is placed in Form Data

The above is all the content of this article, I hope that everyone’s learning is helpful, I hope everyone will support Tumi Cloud.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment