node-cookie-parserDemo
let express = require('express'); let app = new express(); let cookieParser = require('cookie-parser'); app.use(cookieParser()); app.get('/',(req,res)=>{ res.send('您浏览过的城市'+ req.cookies.citys); }); // app.get('/lvyou/:city',(req,res)=>{ // console.log(req.params); // res.send('123'); // }); app.get('/lvyou',(req,res)=>{ let city = req.query.city; let arr = req.cookies.citys; if(arr){ arr.push(city); }else{ arr = []; arr.push(city); } res.cookie('citys',arr,{maxAge:1000*60*10}); res.send(arr); }); app.listen(3000,()=>{ console.log('go'); })
使用cookie来保存一些信息实现城市浏览记录。