简体   繁体   English

NodeJS - 需要错误密码

[英]NodeJS - Error secret required

i am new to working with nodejs and i'm just trying to start a server that is already working with my teammates.我是使用 nodejs 的新手,我只是想启动一个已经与我的队友一起工作的服务器。 I am on a mac and already installed all the needed modules with "npm install".我在 Mac 上,并且已经使用“npm install”安装了所有需要的模块。 Now there seems to be a problem with the module "cookie-signature", which is already included in the "express"-module.现在模块“cookie-signature”似乎有问题,它已经包含在“express”模块中。 I am trying to start the program and i get no error, but when i try to open the page on localhost:3000, i get the following error:我正在尝试启动程序并且没有收到任何错误,但是当我尝试在 localhost:3000 上打开页面时,出现以下错误:

/Users/kevinglaap/Sites/Uni/git/node_server/node_modules/express/node_modules/cookie-signature/index.js:19
  if ('string' != typeof secret) throw new TypeError('secret required');
                                       ^
TypeError: secret required
    at Object.exports.sign (/Users/kevinglaap/Sites/Uni/git/node_server/node_modules/express/node_modules/cookie-signature/index.js:19:40)
    at ServerResponse.end (/Users/kevinglaap/Sites/Uni/git/node_server/node_modules/express/node_modules/connect/lib/middleware/session.js:267:34)
    at ServerResponse.EventEmitter.emit (events.js:93:17)
    at ServerResponse.res.writeHead (/Users/kevinglaap/Sites/Uni/git/node_server/node_modules/express/node_modules/connect/lib/patch.js:73:36)
    at ServerResponse._implicitHeader (http.js:932:8)
    at ServerResponse.OutgoingMessage.end (http.js:767:10)
    at res.end (/Users/kevinglaap/Sites/Uni/git/node_server/node_modules/express/node_modules/connect/lib/middleware/session.js:282:13)
    at /Users/kevinglaap/Sites/Uni/git/node_server/node_modules/express/node_modules/connect/lib/middleware/session/memory.js:73:11
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

The module is never used in the server resources.该模块从不在服务器资源中使用。 I already checked for the usage of the "sign"-function, because the error is a fetched error, which signals that the resources may be doing something wrong, but it is only used by express or other modules within express.我已经检查了“sign”函数的用法,因为错误是一个获取的错误,这表明资源可能做错了什么,但它只被 express 或 express 中的其他模块使用。 I have been searching the web for days now and haven't found a solution yet.我已经在网上搜索了几天,还没有找到解决方案。 What am i missing?我错过了什么? Thanks in advance for your help.在此先感谢您的帮助。

When configuring your express instance you need this:配置 express 实例时,您需要:

app.use(express.cookieParser('your secret option here'));
app.use(express.session());

Making sure your cookieParser (with your secret String) is before the express.session()确保您的 cookieParser(带有您的秘密字符串)在express.session()之前

with the updated express version:使用更新的快速版本:

var app = express();
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));

One should add a secret while creating an instance of express-session in middleware.在中间件中创建 express-session 实例时应该添加一个秘密。

Its always a good practice to have .env file for storing all the secrets not just adding a string.拥有 .env 文件来存储所有秘密,而不仅仅是添加字符串,这始终是一个好习惯。

In .env:在 .env 中:

secret='my_secret'

In server.js:在 server.js 中:

app.use(cookieParser())
app.use(session({
  resave:true,
  saveUninitialized:true,
  secret:process.env.secret,
  cookie:{maxAge:3600000*24}
}))
app.use(session({
    secret: process.env.SECRET,
    reserve: true,
    saveUninitialized: true,
    cookie:{secure: false}
})
)
    .env
PORT=6000
CONNECTION_STRING=mongodb://localhost:27017//bookingApp
SECRET=[thinkbeforactingalwas]
MODE_ENV=development

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:签名cookie需要cookieParser(“secret”) - Error: cookieParser(“secret”) required for signed cookies 错误:配置验证错误:子“JWT_SECRET”失败,因为[需要“JWT_SECRET”] - Error: Config validation error: child “JWT_SECRET” fails because [“JWT_SECRET” is required] MEAN IO:配置验证错误JWT_SECRET是必需的 - MEAN IO : Config validation error JWT_SECRET is required 类型错误:“秘密”是必需的 - TypeError: "secret" is required 错误:配置验证错误:“JWT_SECRET”是必需的。 “JWT_EXPIRATION_TIME”是必需的。 这是什么。 如何避免这种情况 - Error: Config validation error: “JWT_SECRET” is required. “JWT_EXPIRATION_TIME” is required. What is this. How to avoid this 共享 ECDH 密钥,浏览器 + NodeJS - Shared ECDH Secret, Browser + NodeJS 错误:安全node.js coffeescript必需connect.session({secret:“ string”}) - Error: connect.session({ secret: “string” }) required for security node.js coffeescript 在 localhost 中登录,但在 Heroku 中部署时出现“会话所需的秘密选项”错误 - Login working in localhost but error “secret option required for sessions” when deployed in Heroku 无法启动 nodejs 应用程序-错误:需要 npm.load() - unable to start nodejs app- Error: npm.load() required 错误产品验证失败:userId:nodejs a中需要路径`userId` - error Product validation failed: userId: Path `userId` is required in nodejs a
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM