当启动一个新的会话时,例如,当我从incognito标签访问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);
});
});
}
如果这个问题措辞过于宽松,请原谅,但我很不确定是什么原因导致了这个错误。
return User.findById(id, function (err, user) {
return done(err, user);
});
});
你不能把return语句变成return语句,因为只有return语句。!!!
修正了
之前,我是使用
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();
}
})