node.js Express.js 的仅会话 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4371178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:45:31  来源:igfitidea点击:

Session-only cookie for Express.js

node.jssession-cookiesexpress

提问by Alfred

http://www.javascriptkit.com/javatutors/cookie.shtml

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.

另一方面,仅会话 cookie 将信息存储在浏览器内存中,并且在浏览器会话期间可用。换句话说,会话 cookie 中存储的数据从存储时间到浏览器关闭都是可用的。在此期间逐页移动不会擦除数据。

How can I achieve this using Express.js?

如何使用Express.js实现这一点?

采纳答案by Alfred

The following is what I wanted (sort of). When I close browser the information is gone.

以下是我想要的(有点)。当我关闭浏览器时,信息消失了。

var express = require('express');
var app = express.createServer();

var MemoryStore = require('connect/middleware/session/memory');
app.use(express.bodyDecoder());
app.use(express.cookieDecoder());

app.get('/remember', function(req, res) {
    res.cookie('rememberme', 'yes', { expires: new Date() - 1, httpOnly: true });
});

app.get('/', function(req, res){
    res.send('remember: ' + req.cookies.rememberme);
});

app.listen(4000, '127.0.0.1');

回答by Ivo Wetzel

First off, that website is a horrible place to go.

首先,该网站是一个可怕的地方。

Now on to the question.

现在来回答这个问题。

What sessions actually are:

什么会话实际上是:

  • Data is stored on the server side.
  • A cookie is issued which contains an ID.
  • This ID gets send back to the server on every request, due to the fact that the browser sends the cookies.
  • Now the server can re-associate the ID in the cookie - commonly called Session IDor short SID- with the session data stored on the server.
  • 数据存储在服务器端。
  • 发出包含 ID 的 cookie。
  • 由于浏览器发送 cookie,这个 ID 会在每次请求时发送回服务器。
  • 现在,服务器可以将 cookie 中的 ID(通常称为Session ID或简称SID)与存储在服务器上的会话数据重新关联。

Express.js has support for sessions built in.

Express.js 支持内置于.

What the example shows:

该示例显示的内容:

  • Setting up the Express.js middleware
  • Using a third-party store for saving the session data, in this case Redis(which IMO is overkill for your problem atm)
  • 设置 Express.js 中间件
  • 使用第三方存储来保存会话数据,在这种情况下是Redis(IMO 对您的问题 atm 来说太过分了)

Installing Redis requires quite some work, but it's also possible to use Express.js's built-in memory store:

安装 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);

This will simply keep track of how many times you visited the page, closed your browser and re-opend. The counts will still be there.

这将简单地记录您访问该页面的次数、关闭浏览器并重新打开的次数。计数仍然存在。

You can find more on the options of the MemoryStore, like maximum life time of a session, etc. here.

您可以在此处找到有关 选项的更多信息MemoryStore,例如会话的最长生命周期等。

回答by Ustaman Sangat

app.use(express.session({cookie: { path: '/', httpOnly: true, maxAge: null }, secret:'eeuqram'}));

The above works on IE8, Firefox and Chrome. The important piece is maxAge:null

以上适用于 IE8、Firefox 和 Chrome。重要的是maxAge:null

回答by Raja

app.get('/remember', function(req, res) {
   res.cookie('rememberme', 'yes', { expires: 0, httpOnly: true });
 });

This will set session cookie. On browser close it will be erased!

这将设置会话cookie。在浏览器关闭时,它将被删除!

回答by Raja

Below is the updated code for Alfred's answer(session using Express.js).

下面是Alfred 答案的更新代码(使用 Express.js 的会话)。

    var express = require('express');
    var app = express.createServer();

    var MemoryStore = require('/home/node/node_modules/connect/lib/middleware/session/memory');
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({
        key: 'some-key',
        secret: 'some-We1rD sEEEEEcret!',
        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);