启动新会话会导致错误-在将标头发送到客户端后无法设置标头



启动新会话时,例如,当我从隐姓埋名选项卡访问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:DocumentsProjectsTypeTestnode_modulesexpresslibresponse.js:771:10)
at ServerResponse.send (E:DocumentsProjectsTypeTestnode_modulesexpresslibresponse.js:170:12)
at done (E:DocumentsProjectsTypeTestnode_modulesexpresslibresponse.js:1008:10)
at tryHandleCache (E:DocumentsProjectsTypeTestnode_modulesejslibejs.js:261:5)
at View.exports.renderFile [as engine] (E:DocumentsProjectsTypeTestnode_modulesejslibejs.js:461:10)
at View.render (E:DocumentsProjectsTypeTestnode_modulesexpresslibview.js:135:8)
at tryRender (E:DocumentsProjectsTypeTestnode_modulesexpresslibapplication.js:640:10)
at Function.render (E:DocumentsProjectsTypeTestnode_modulesexpresslibapplication.js:592:3)
at ServerResponse.render (E:DocumentsProjectsTypeTestnode_modulesexpresslibresponse.js:1012:7)
at E:DocumentsProjectsTypeTestnode_modulesexpress-ejs-layoutslibexpress-layouts.js:113:20
at tryHandleCache (E:DocumentsProjectsTypeTestnode_modulesejslibejs.js:261:5)
at View.exports.renderFile [as engine] (E:DocumentsProjectsTypeTestnode_modulesejslibejs.js:461:10)
at View.render (E:DocumentsProjectsTypeTestnode_modulesexpresslibview.js:135:8)
at tryRender (E:DocumentsProjectsTypeTestnode_modulesexpresslibapplication.js:640:10)
at Function.render (E:DocumentsProjectsTypeTestnode_modulesexpresslibapplication.js:592:3)

我知道多次调用res.render()res.send()等会导致此错误,但我在代码中找不到任何此类示例。正如我所提到的,这种情况只发生在启动一个全新的会话时(我认为(,例如在匿名选项卡中访问网站时。这让我相信这与cookie会话有关——也许是重新呈现页面?

即使我让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);
});
});
}

如果这个问题措辞过于松散,我深表歉意,但我不确定是什么导致了这个错误。感谢

return User.findById(id, function (err, user) {
return done(err, user);
});

});

您不能在返回语句中使用返回语句,因为只有返回语句!!!

固定

在我使用将HTTP请求重定向到HTTPS服务器之前

app.use((req, res, next) => {
if (req.protocol === 'http') {
res.redirect(301, `https://${req.headers.host}${req.url}`);
}
next();
})

这导致任何HTTP请求都要处理两次。我只是在使用隐姓埋名选项卡时才遇到这种情况,因为在常规选项卡中,Chrome在发出请求之前会自动切换到HTTPS。通过修复

app.use((req, res, next) => {
if (req.protocol === 'http') {
res.redirect(301, `https://${req.headers.host}${req.url}`);
} else {
next();
}
})

最新更新