nodejs中express框架里服务器session数据库存储(mongodb)实现方法
分享知识http://www.fedrobots.com/?search=229842我来纠错//express框架之session 内存存储
express-session 是基于express框专门用于处理session的中间件。这里不谈express-session怎么安装,只给出相应的实例代码。另外,session的认证机制离不开cookie,需要同时使用cookieParser 中间件。
<script>
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
app.use(session({
secret: '12345',
name: 'testapp', //这里的name值得是cookie的name,默认cookie的name是:connect.sid
cookie: {maxAge: 80000 }, //设置maxAge是80000ms,即80s后session和相应的cookie失效过期
resave: false,
saveUninitialized: true,
}));
app.get('/awesome', function(req, res){
if(req.session.lastPage) {
console.log('Last page was: ' + req.session.lastPage + ".");
}
req.session.lastPage = '/awesome'; //每一次访问时,session对象的lastPage会自动的保存或更新内存中的session中去。
res.send("You're Awesome. And the session expired time is: " + req.session.cookie.maxAge);
});
app.get('/radical', function(req, res){
if (req.session.lastPage) {
console.log('Last page was: ' + req.session.lastPage + ".");
}
req.session.lastPage = '/radical';
res.send('What a radical visit! And the session expired time is: ' + req.session.cookie.maxAge);
});
app.get('/tubular', function(req, res){
if (req.session.lastPage){
console.log("Last page was: " + req.session.lastPage + ".");
}
req.session.lastPage = '/tubular';
res.send('Are you a suffer? And the session expired time is: ' + req.session.cookie.maxAge);
});
app.listen(3000);
</script>
//express框架之session 数据库存储
有时候,我们需要session的声明周期要长一点,比如好多网站有个免密码两周内自动登录的功能。基于这个需求,session必须寻找内存之外的存储载体,数据库能提供完美的解决方案。这里,我选用的是mongodb数据库,作为一个NoSQL数据库,它的基础数据对象时database-collection-document 对象模型非常直观并易于理解,针对node.js 也提供了丰富的驱动和API。express框架提供了针对mongodb的中间件:connect-mongo,我们只需在挂载session的时候在options中传入mongodb的参数即可,程序运行的时候, express app 会自动的替我们管理session的存储,更新和删除
<script>
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongo')(session);
var app = express();
app.use(cookieParser());
//跟session的内存存储一样,只需增加store选项即可,app会自动替我们把session存入到mongodb数据,而非内存中。
app.use(session({
secret: '123456',
name: 'test',
cookie: {maxAge: 80000 },
resave: false,
saveUninitialized: true,
store: new MongoStore({ //创建新的mongodb数据库
host: 'localhost', //数据库的地址,本机的话就是127.0.0.1,也可以是网络主机
port: 27017, //数据库的端口号
db: 'test-app' //数据库的名称。
})
}));
app.get('/awesome', function(req, res){
if(req.session.lastPage) {
console.log('Last page was: ' + req.session.lastPage + ".");
}
req.session.lastPage = '/awesome';
res.send("You're Awesome. And the session expired time is: " + req.session.cookie.maxAge);
});
app.get('/radical', function(req, res){
if (req.session.lastPage) {
console.log('Last page was: ' + req.session.lastPage + ".");
}
req.session.lastPage = '/radical';
res.send('What a radical visit! And the session expired time is: ' + req.session.cookie.maxAge);
});
app.get('/tubular', function(req, res){
if (req.session.lastPage){
console.log("Last page was: " + req.session.lastPage + ".");
}
req.session.lastPage = '/tubular';
res.send('Are you a suffer? And the session expired time is: ' + req.session.cookie.maxAge);
});
app.listen(5000);
</script>