In the background with nodejs+express+mysql, there are many requests involving user information to determine whether the token in the request (user name + timestamp + random number using SHA1 encryption of random string) is valid.
In the beginning, the judgment of token was added to the processing of each request. (wrote n times, the head smoked)
The code is as follows:
Modify the state of the Project Router.post ('/change-project ', validtoken,function (req,res,next) {var db = req.db; var token = Req.query.token; var id = req.query.id; var projectstatus = Req.query.status; var data = {status:false, message: ""} db.getconnection (function (err,conn) {if (err) { SendData (Req,res,next,conn,err); }else {db.query (' SELECT * from user WHERE User_token = ' +token+ ', function (err,row) {//Determine if token is valid if (err) {senddata (req,res,next,conn,err); }else{if (row.length = = 0) {senddata (req,res,next,conn, "please login");//Invalid return error message }else {//If token is effectively modified state db.query (' UPDATE project SET project_status = ' + Projectstatus + ' WHERE project_id = ' + id + ', function (err, row) {if (err) { SendData (req, Res, NEXT, Conn, err); } else {data.message = (Row.affectedrows = = 1)? "Modification succeeded": "Modification Failed"; Data.status = (Row.affectedrows = = 1)? True:false; Res.send ({' Data ': Data}); Conn.release (); } }) } } }) } })})
The place where token verification is involved is relatively redundant, because it is written again in every function that needs to be validated.
So this part of validating token is proposed to become a middleware
The code is as follows:
function Validtoken (req, res, next) { var db = req.db; var usertoken = Req.query.token; Db.getconnection (function (err,conn) { if (err) { senddata (req,res,next,conn,err); } else{ db.query (' SELECT * from user WHERE User_token = ' +usertoken+ ', function (err,row) { if (err) { SendData (Req,res,next,conn,err); } else{ if (row.length = = 0) { senddata (req,res,next,conn, "please login"); } else{ Next ();}})} )} Returns a data object when an error function SendData (Req,res,next, conn,message) { var data = { message: "",//error message Status: False//Status } data.message = message; Conn.release (); Res.send ({"Data": Data});}
The middleware is then applied to the corresponding route,
So the first route becomes, the following sample paper:
Modify the state of the Project Router.post ('/change-project ', validtoken,function (req,res,next) { var db = req.db; var token = Req.query.token; var id = req.query.id; var projectstatus = req.query.status; var data = { Status:false, message: "" } db.getconnection (function (err,conn) { if (err) { SendData (Req,res,next,conn,err); } else { db.query (' UPDATE project SET project_status = ' + projectstatus + ' WHERE project_id = ' + id + ', function ( Err, row) { if (err) { senddata (req, Res, NEXT, Conn, err); } else { data.message = (row.affectedrows = = 1)? "Modification succeeded": "Modification failed"; Data.status = (Row.affectedrows = = 1)? True:false; Res.send ({' Data ': Data}); Conn.release (),})})
Such other needs to verify token can not be repeated in the write, it is directly reusable.
In fact, a layer of nested (if (ERR) error-handling else queries about querying the database can also be addressed through the middleware.
Specifically, you can read this blog: http://www.360doc.com/content/14/1003/20/14106735_414210206.shtml
Sleep and have a Yan feel go ~ ~
Using middleware to improve code reuse in Nodejs+express