Session
Mounting module
CNPM Install Express-session
Introduce session registration to routing
var express = require (' Express '); var session = require (' express-session '); var router =falsetrue, cookie: {maxage:60000}});
Operation Session
function (req, res, next) { // set session req.session.username = ' Zhangsan '; // Get Session Console.log (req.session); Console.log (req.session.myname); Res.render ('/index ', {title: ' Home '}) ;
Sessions exist in the session, connect a break, the session disappears, so use the session in the development phase please be prepared to be mad.
Cookies:
Mounting module
CNPM Install Cookie-parser
Introducing Cookies
var express = require (' Express '); var cookie = require (' Cookie-parser '); var router = Express. Router ();
Manipulating cookies
function (req, res, next) { // Set cookie Res.cookie (' username ', ' Zhangsan '); Res.cookie (// valid in milliseconds // get cookie console.log ( req.cookies); Console.log (req.cookies.username); // Delete Cookies Res.clearcookie (' username '); Res.render ('/index ', {title: ' Home '}) ;
Nodejs Operating session and cookies