Nodejs Operating session and cookies

Source: Internet
Author: User
Tags set cookie

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.