请考虑简单的express.js路线:
app.get("/test", function(req, res) { if(condition1) { res.render("foo", { title : "Test" }); } console.log("HERE"); if(condition2) { res.render("bar", { title : "Test2" }); } });现在,在这个人为的例子中,有时condition1将是真的,有时则是condition2。 但是,在condition1为true的情况下,控件将通过并打印出console.log行,然后在遇到另一个渲染时失败:
错误:发送后无法设置标头。
这就像响应需要结束或其他什么,但我已经尝试过,无济于事。 任何帮助,将不胜感激。 谢谢。
Please consider the simple express.js route:
app.get("/test", function(req, res) { if(condition1) { res.render("foo", { title : "Test" }); } console.log("HERE"); if(condition2) { res.render("bar", { title : "Test2" }); } });Now, in this contrived example, sometimes condition1 will be true, and other times, condition2. However, in the case that condition1 is true, the control passes through and prints out the console.log line and then fails when it hits the other render with:
Error: Can't set headers after they are sent.
It's like the response needs to be ended or something, but I've tried that, to no avail. Any help would be appreciated. Thanks.
最满意答案
我只是使用return res.render() 。
当你说它不起作用时很奇怪,因为返回确实应该阻止函数的其余部分执行。
I just use return res.render().
It's strange when you say it doesn't work, because return really should stop the rest of the function from executing.
更多推荐
发布评论