如何在Express.JS中渲染Jade模板后返回?(How to return after rendering Jade template in Express.JS?)

编程入门 行业动态 更新时间:2024-10-21 15:56:02
如何在Express.JS中渲染Jade模板后返回?(How to return after rendering Jade template in Express.JS?)

请考虑简单的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.

更多推荐

本文发布于:2023-07-22 09:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   如何在   JS   Jade   Express

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!