javascript Node JS 会话错误:不推荐使用 express-session

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28839532/
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-10-28 09:38:13  来源:igfitidea点击:

Node JS session error: express-session deprecated

javascriptnode.jssessionexpress

提问by Arian Faurtosh

I have the following in my node js file

我的节点 js 文件中有以下内容

var express = require('express');
var util = require('./lib/utility');
var partials = require('express-partials');
var bodyParser = require('body-parser');

var session = require('express-session');

/* abstracted some code */

app.use(session({
  genid: function(req) {
    return genuuid(); // use UUIDs for session IDs
  },
  secret: 'keyboard cat'
}));

When I start the server, I get

当我启动服务器时,我得到

express-session deprecated undefined resave option; provide resave option server.js
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option server.js

I am having a hard time figuring out what is deprecated? I copied the example from https://github.com/expressjs/session

我很难弄清楚什么是弃用的?我从https://github.com/expressjs/session复制了这个例子

When I try to load the page I get:

当我尝试加载页面时,我得到:

ReferenceError: genuuid is not defined at app.use.session.genid

ReferenceError:genuuid 未在 app.use.session.genid 中定义

回答by mscdex

You have to define the genuuidfunction somewhere. The express-sessionreadme is assuming you have already implemented that.

您必须在genuuid某处定义函数。该express-session自述是假设你已经实施了。

Regarding the warnings, you need to explicitly set the resaveand saveUninitializedoptions in your session configuration object.

关于警告,您需要在会话配置对象中明确设置resavesaveUninitialized选项。

回答by Darryl Snow

From express 4.0 , express-session with odd warning message

express 4.0 开始,带有奇怪警告消息的 express-session

As the warnings say, the default values will change so they want to ensure that by setting the values explicitly now, you won't run into unexpected behavior when the defaults do change (in the near future).

正如警告所说,默认值会发生变化,因此他们希望确保通过现在明确设置值,当默认值确实发生变化时(在不久的将来),您不会遇到意外行为。

app.use(session({
    secret: cookie_secret,
    resave: true,
    saveUninitialized: true
}));

回答by Asif

Add these flags-

添加这些标志-

resave: true,
saveUninitialized: true

inside your - app.use(session({});and it should work fine.

在您的内部 - app.use(session({});它应该可以正常工作。

回答by Rajesh Kumar

app.use(session({ secret: 'anything', resave: true, saveUninitialized: true }));/

回答by Gajen Sunthara

if you are using maxAgefor session cookie duration:

如果您使用maxAge会话 cookie 持续时间:

app.use(session({
  secret: '<session_secret>', 
  resave: true, 
  saveUninitialized: true,
  maxAge: 3600000   // 1 hour (in milliseconds)
})); 

回答by Ravi Chandola

Issue - express-session deprecated undefined saveUninitialized option

问题 - express-session 已弃用未定义的 saveUninitialized 选项

Issue - provide saveUninitialized option

问题 - 提供 saveUninitialized 选项

Use this , hope it will help

使用这个,希望它会有所帮助

app.use(session({
    name : 'codeil',
    secret : 'something',
    resave :false,
    saveUninitialized: true,
    cookie : {
            maxAge:(1000 * 60 * 100)
    }      
}));

回答by mohamed ibrahim

You have to use a new version of expressand define genid function somewhere in your app or use uuidpackage instead

您必须使用新版本的express并在您的应用程序中的某处定义 genid 函数或使用uuid包代替