If I want to avoid lots of else
blocks and deep indentations, what should be the return type if I want to exit a router function explictly?
app.get("/xx", function(req, res) {
if (c1) {
res.render("c1");
return ??;
}
if (c2) {
res.render("c2");
return ??;
}
res.render("default");
})
Santiago Trujillo
It doesn't really matter. This is an asynchronous function, and nobody's going to use this return value, so you can just return undefined
:
res.render(...);
return;
As it doesn't matter, you can also write:
return res.render(...);
But I think it looks more readable in two lines.
Instead of res.render()
you should be using res.send()
, as it will terminate the response stream with the output sent.