ERR_HTTP_HEADERS_SENT - Express:标头在发送到客户端后无法设置,我该如何解决这个错误?
Posted
技术标签:
【中文标题】ERR_HTTP_HEADERS_SENT - Express:标头在发送到客户端后无法设置,我该如何解决这个错误?【英文标题】:ERR_HTTP_HEADERS_SENT - Express: headers cannot be set after they are sent to the client, how can i solve this error? 【发布时间】:2020-05-15 05:39:45 【问题描述】:我已经在 *** 上尝试了很多不同的解决方案,但是。但他们没有工作。
它引发了这个错误:
错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 在 ServerResponse.setHeader (_http_outgoing.js:485:11) 在 ServerResponse.location (/mnt/c/Users/Lucas-PC/) 的 ServerResponse.header (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/response.js:771:10) Desktop/OnlineChat/node_modules/express/lib/response.js:888:15) 在 ServerResponse.redirect (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/response.js:926: 18) 在声明中。 (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/app.js:135:28) --> 在 Database#each("SELECT * FROM users WHERE email='yyyy@yyyy'", [Function]) 在 /mnt/c/Users/Lucas-PC/Desktop/OnlineChat/app.js:133:16 在 Layer.handle [as handle_request] (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/layer.js:95:5) 在下一个(/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/route.js:137:13) 在 Route.dispatch (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/route.js:112:3) 在 Layer.handle [as handle_request] (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/layer.js:95:5) 在 /mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/index.js:281:22 在 Function.process_params (/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/index.js:335:12) 在下一个(/mnt/c/Users/Lucas-PC/Desktop/OnlineChat/node_modules/express/lib/router/index.js:275:10) 在 /mnt/c/Users/Lucas-PC/Desktop/OnlineChat/app.js:70:5 代码:'ERR_HTTP_HEADERS_SENT', __augmented:是的
这是我的代码:
const redirectLogin = (req, res, next) =>
if (!req.session.userId)
res.redirect('/login');
else
next();
const redirectHome = (req, res, next) =>
if (req.session.userId)
res.redirect('/dashboard');
else
next();
app.post('/login', redirectHome, (req, res) =>
reloadUsers(users);
const email, password = req.body;
if (email && password)
const user = users.find(user => user.email === email && user.password === password) // TODO: hash password
if (user)
console.log(user.id);
req.session.userId = user.id;
res.redirect('/dashboard');
return;
res.redirect('/')
)
app.post('/register', redirectHome, (req, res) =>
const name, job, email, password = req.body;
if (name && job && email && password) // TODO: validation more complex
const exists = users.some(user => user.name === name && user.email === email);
if(!exists)
let infos = [name, email, job, password];
let placeholders = infos.map((info) => '?').join(',');
let sql = 'INSERT INTO users(name, email, job, password) VALUES('+ placeholders +')';
db.run(sql, infos);
console.log('email: '+req.body.email);
db.each("SELECT * FROM users WHERE email='"+req.body.email+"'", (err, row) =>
req.session.userId = row.id;
)
return res.redirect('/dashboard');
res.redirect('/');
)
app.post('/logout', redirectLogin, (req, res) =>
reloadUsers(users);
req.session.destroy(err =>
if (err)
return res.redirect('/dashboard');
res.clearCookie(SESS_NAME);
res.redirect('/login');
);
)
我认为错误来自res.redirect('/dashboard')
上的app.post('/login')
和app.post('/register')
【问题讨论】:
【参考方案1】:在您的/register
路由器下,您尝试在发送响应后设置请求。
db.each("SELECT * FROM users WHERE email='"+req.body.email+"'", (err, row) =>
req.session.userId = row.id;
)
return res.redirect('/dashboard');
db.each()
是一个异步函数,因此您必须在函数内处理响应。
db.each("SELECT * FROM users WHERE email='"+req.body.email+"'", (err, row) =>
req.session.userId = row.id;
return res.redirect('/dashboard');
)
一些提示:在构造 SQL 语句时,还要尝试转义字符串以避免 SQL 注入。
【讨论】:
以上是关于ERR_HTTP_HEADERS_SENT - Express:标头在发送到客户端后无法设置,我该如何解决这个错误?的主要内容,如果未能解决你的问题,请参考以下文章
Express.js 中的新 Mongoose Schema `ERR_HTTP_HEADERS_SENT` 错误
错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法删除标头
我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误?
我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误?