【发布时间】:2020-05-07 20:57:48
【问题描述】:
开始新会话时,例如当我从隐身选项卡访问 localhost 时,我收到以下错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (E:\Documents\Projects\TypeTest\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (E:\Documents\Projects\TypeTest\node_modules\express\lib\response.js:170:12)
at done (E:\Documents\Projects\TypeTest\node_modules\express\lib\response.js:1008:10)
at tryHandleCache (E:\Documents\Projects\TypeTest\node_modules\ejs\lib\ejs.js:261:5)
at View.exports.renderFile [as engine] (E:\Documents\Projects\TypeTest\node_modules\ejs\lib\ejs.js:461:10)
at View.render (E:\Documents\Projects\TypeTest\node_modules\express\lib\view.js:135:8)
at tryRender (E:\Documents\Projects\TypeTest\node_modules\express\lib\application.js:640:10)
at Function.render (E:\Documents\Projects\TypeTest\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (E:\Documents\Projects\TypeTest\node_modules\express\lib\response.js:1012:7)
at E:\Documents\Projects\TypeTest\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
at tryHandleCache (E:\Documents\Projects\TypeTest\node_modules\ejs\lib\ejs.js:261:5)
at View.exports.renderFile [as engine] (E:\Documents\Projects\TypeTest\node_modules\ejs\lib\ejs.js:461:10)
at View.render (E:\Documents\Projects\TypeTest\node_modules\express\lib\view.js:135:8)
at tryRender (E:\Documents\Projects\TypeTest\node_modules\express\lib\application.js:640:10)
at Function.render (E:\Documents\Projects\TypeTest\node_modules\express\lib\application.js:592:3)
我知道多次调用res.render() 或res.send() 等会导致此错误,但我在我的代码中找不到任何此类示例。正如我所提到的,这只发生在开始一个全新的会话时(我认为),例如在隐身标签中访问该站点时。这让我相信它可能与 cookie-session 重新呈现页面有关?
即使我将get 路由器完全留空并且从未发送任何信息,我也会收到相同的错误。这再次让我相信它与我的会话中间件有关。我也在使用 Passport 进行身份验证,但我认为这不是问题的原因。
这是server.js 的一部分,其中包括会话中间件。
app.set('view engine', 'ejs');
app.set('views', './views');
app.set('layout', 'layouts/default')
app.use(expressLayouts);
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const flash = require('express-flash');
const cookieSession = require('cookie-session');
app.use(flash());
app.use(cookieSession({
name: 'session',
secret: process.env.SESSION_SECRET,
maxAge: 10000000 * 60 * 60 * 24
}));
const passport = require('passport');
const initializePassport = require('./config/passport');
initializePassport(passport);
app.use(passport.initialize());
app.use(passport.session());
这是我的 Passport 配置。文件
module.exports = function (passport) {
passport.use(
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
User.findOne().byEmailOrUsername(email).then(user => {
if (!user) {
return done(null, false, { message: 'Incorrect email or password' })
}
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) {
return done(err);
} else if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: 'Incorrect email or password' });
}
});
})
}));
passport.serializeUser(function (user, done) {
console.log('serialize');
return done(null, user.id);
});
passport.deserializeUser(function (id, done) {
console.log('deserialize');
return User.findById(id, function (err, user) {
return done(err, user);
});
});
}
如果这个问题措辞过于松散,我们深表歉意,但我非常不确定是什么导致了这个错误。 谢谢
【问题讨论】:
标签: node.js express session passport.js