This article describes the method of nodejs using express to obtain get and post pass values and session verification. Share it for your reference, as follows:
Get the get and post pass values
The value of get passed is put into an object
The value of the post has been put into
The way to obtain the object's content is the same. For example, a value of id is passed in front, and nodejs can be retrieved.
Express session verification
The first step is to install the cookie and session module and introduce it
var session = require('express-session'); var cookieParser = require('cookie-parser');
Part 2, Express app cookies and session
(cookieParser()); (session({ resave: true, // don't save session if unmodified saveUninitialized: false, // don't create session until something stored secret: 'admin', //Key name: 'testapp', //The name here is worth the name of the cookie, and the default name of the cookie is: cookie: { maxAge: 80000 } //Set maxAge to 80000ms, that is, the session and corresponding cookies expire after 80s}));
Step 3: Intercept processing when requesting
(function(req, res, next) { if (!) { if ( == "/login") { next(); //If the requested address is login, pass and make the next request } else { ('/login');//Skip to login page } } else if () { next();//If you are already logged in, you can enter } });
Now if you are not logged in to access the page, the route will be automatically pointed to the /login page. The last step is to handle it in the route.
('/login', function(req, res) { ("login"); }); ('/login', function(req, res) { if () {//There is a value passed when judging var user = { 'username': //Get the user name and assign the value. You can make your own judgment before this }; = user;//Assign session, automatically jump to the page ('/admin'); } else { ('/login'); } }); ('/logout', function(req, res) {//Done login page = null; ('/login'); });
I hope this article will be helpful to everyone's nodejs programming.