问题描述
我是 Express
的新手。由于 Express 4.x
已删除捆绑的中间件。
我需要使用任何中间件。当我阅读和,我很难理解它们之间的区别。
I am new with Express
. As Express 4.x
has removed bundled middlewares.Any middleware I want to use should be required. When I read the README with express-session and cookie-session on github, I feel it hard to understand the difference.
所以我尝试编写简单的代码来弄清楚。我为每个中间件运行两次。
So I try to write simple code to figure it out. I run twice for each middleware.
var express = require('express')
, cookieParser = require('cookie-parser')
, session = require('cookie-session')
, express_sess = require('express-session')
, app = express();
app.use(cookieParser())
app.use(session({ keys: ['abc'], name: 'user' }));
//app.use(express_sess({ secret: 'abc', key: 'user'}));
app.get('/', function (req, res, next) {
res.end(JSON.stringify(req.cookies));
console.log(req.session)
console.log(req.cookies)
});
app.listen(3000);
对于 cookie会话
,我总是得到{}在我的终端中。
For cookie-session
, I always get {} in my terminal.
对于 express-session
,我会得到类似的东西。
For express-session
, I get things like this.
req.session: { cookie: {
path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true
}
}
req.cookie: {user: 's:aJ97vKA5CCwxqdTj0AV1siRQ.fWusS5+qfCKICtwkfrzcZ/Gq8P0Qdx/kx8mTBhoOhGU'}
这真的让我感到困惑。那么如何用基本用法来解释结果呢?它们之间有什么区别?我什么时候应该使用它们?
It really confuses me. So how to explain the result with the basic use? And what's the difference between them? When should I use them?
推荐答案
基本上, express-session
更抽象,它支持不同的会话存储(例如文件,数据库,缓存和诸如此类的东西)。
Basically, express-session
is more abstract, it supports different session stores (like files, DB, cache and whatnot).
而 cookie-session
是简单/轻量级的基于cookie(会话是唯一支持的存储引擎:所有会话信息都存储在客户端的cookie中)。这种会话可能以而闻名。
And cookie-session
is a simple / lightweight cookie-based (cookie is the only storage engine supported: all the session info is stored on the client, in a cookie) session implementation. This kind of sessions is probably most famous for its Rails implementation.
这篇关于Express会话和Cookie会话有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!