http://www.javascriptkit.com/javatutors/cookie.shtml
Session-only cookies,on the other
hand,stores information in the
browser memory,and is available for
the duration of the browser session.
In other words,the data stored inside
a session cookie is available from the
time of storage until the browser is
closed. Moving from page to page
during this time does not erase the
data.
如何使用Express.js?
解决方法
首先,该网站是一个可怕的地方去。
现在就问题了。
实际上是什么?
>数据存储在服务器端。
>发出一个包含ID的cookie。
>由于浏览器发送cookies的事实,每个请求都会将该ID发送回服务器。
>现在,服务器可以将Cookie中的ID(通常称为会话ID或短SID)与服务器上存储的会话数据重新关联。
Express.js has support for sessions built in。
示例显示:
>设置Express.js中间件
>使用第三方存储来保存会话数据,在这种情况下为Redis(哪个IMO对于您的问题atm而言是过度的)
安装Redis需要相当的工作,但也可以使用Express.js的内置内存存储:
var express = require('express'); var app = express.createServer(); var MemoryStore = require('connect/middleware/session/memory'); app.use(express.bodyDecoder()); app.use(express.cookieDecoder()); app.use(express.session({ store: new MemoryStore({ reapInterval: 60000 * 10 }) })); app.get('/',function(req,res){ req.session.visitCount = req.session.visitCount ? req.session.visitCount + 1 : 1; res.send('You have visited this page ' + req.session.visitCount + ' times'); }); app.listen(4000);