var express = require('express') var cookieParser = require('cookie-parser') var app = express() app.use(cookieParser()) //不使用签名 //app.use(cookieParser("helowe2")) 若需要使用签名功能,需要指定一个secret字符串,否则会报错。 app.get('/', function (req, res) { // Cookies that have not been signed var html = "<form action='/login'>" html += "<input type='text'>" html += "<input type='submit'>" html += "</form>" html += "<form action='/clear'>" html += "<input type='submit' value='销毁'>" html += "</form>" html += "<form action='/look'>" html += "<input type='submit' value='查看'>" html += "</form>" res.send(html); }) app.get('/login', function (req, res) { res.cookie('name',"jinkang" {maxAge: 1000*60*60*24*30}); //设置 res.cookie('age',"190"); console.log('Signed Cookies: ', req.signedCookies); console.log('Cookies: ', req.cookies) res.status(200).send("已经设置COOKIE"); }) app.get('/clear', function (req, res) { // Cookies that have not been signed for(x in req.cookies){ res.clearCookie(x); } // 清除cookie //res.clearCookie("name"); 清除浏览器cookie中的name res.status(200).send("CLEAR"); }) app.listen(8080) app.get('/look', function (req, res) { console.log('Signed Cookies: ', req.signedCookies) console.log('COOKIE: ', req.cookies); res.status(200).send("查看cook"); })