将标题发送到客户端nodejs重定向后无法设置标头
我有一些如果/其他统计数字,在if statament中,我可以重定向,但否则我不能。 我使用的代码:
app.use(express.static(__dirname + '/public'));
router.get('/',function(req,res) {
res.sendFile(path.join(__dirname + '/public/html/index.html'));
var email = req.query.email;
conn.query("SELECT * FROM users WHERE email = ?", [email], function(err, result){
if (err) throw err;
var resultLength = result.length;
if (email != null && resultLength == 0) {
conn.query('INSERT INTO users (email) VALUES (?)', [email], function(err, result) {
if (err) throw err;
console.log('1 record inserted')
res.write('<p>You are now subscribed to our newsletter!</p>');
})
res.redirect('/succes')
}else{
console.log("You are already subscribed to our newsletter!")
res.redirect('/error')
}
})
});
I have some IF/Else stataments, in IF statament I could make an redirect, but in else statament I couldn't.
Code that I use:
app.use(express.static(__dirname + '/public'));
router.get('/',function(req,res) {
res.sendFile(path.join(__dirname + '/public/html/index.html'));
var email = req.query.email;
conn.query("SELECT * FROM users WHERE email = ?", [email], function(err, result){
if (err) throw err;
var resultLength = result.length;
if (email != null && resultLength == 0) {
conn.query('INSERT INTO users (email) VALUES (?)', [email], function(err, result) {
if (err) throw err;
console.log('1 record inserted')
res.write('<p>You are now subscribed to our newsletter!</p>');
})
res.redirect('/succes')
}else{
console.log("You are already subscribed to our newsletter!")
res.redirect('/error')
}
})
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向给定请求发送一个和一个回复。当您这样做时:
这会发送对传入请求的回复。试图发送任何其他请求将导致关于“已经发送后无法发送标头”的错误。
但是,在您的代码中,您尝试同时进行
res.write(...)
和res.redirect(...)
。你根本做不到。您可以向给定的传入请求发送一个和一个回复。您已经发送了一个res.sendfile(...)
。然后,更进一步,您不能执行
res.write(...)
,然后进行res.Res.Resc.Redirect(...)
。执行res.write(...)
表示HTTP库必须发送标头,以便现在可以开始发送正文。因此,这迫使标题被发送。但是,现在,当您进行res.redirect(...)
时,您正在告诉Express Library您要设置302状态代码,并且要设置location < /代码>标题。嗯,但是状态代码已经发送,并且已经发送了标题,并被
res.write(...)
迫使。因此,此时,res.redirect(...)
无法完成其工作。目前尚不清楚该代码应该做什么。您要么服务
index.html
,要么正在做其他将其重定向到其他URL的事情。同一路线不能执行这两个操作。对于这样的路线,您要么发送HTML响应,要么将其重定向到另一个URL。选择一个。这是一种仅包含
res.redirect()
的解决方案:You can send ONE and only one response to a given request. When you do:
that sends a response to the incoming request. Attempting to send ANY other request will cause the error about "cannot send headers after they are already sent".
Yet, in your code, you then try to do both
res.write(...)
andres.redirect(...)
. You simply can't do that. You get to send one and only one response to a given incoming request. You already sent one withres.sendFile(...)
.Then, even further, you can't do a
res.write(...)
, then followed byres.redirect(...)
. Doing ares.write(...)
means that the http library has to send the headers so that it can now start sending the body. So, this forces the headers to get sent. But, now when you do ares.redirect(...)
, you're telling the Express library that you want to set a 302 status code and you want to set theLocation
header. Hmmm, but the status code has already been sent and the headers have already been sent, forced out by theres.write(...)
. So, at this point,res.redirect(...)
cannot do its job.It's unclear what this code is supposed to do. You're either serving
index.html
or you're doing something else that redirects to a different URL. The same route can't do both of those operations. For a route like this, you either send an HTML response OR you redirect to another URL. Pick one.Here's one possible solution that contains only
res.redirect()
: