0101 0101 - 6 months ago 42
Node.js Question

ExpressJS - Recommended way of defining routes

I am stuck with kind of a silly thing. There are two possible options (actions) that the user can choose. How should the routes be defined or what's the recommended way of defining in this case?



Variant 1: Keeping every route separate on a new line

app.VERB("/email/register", SomeObject.register);
app.VERB("/email/verify", SomeObject.verify);

Variant 2: Keeping all the options in one block (callback)

app.VERB("/email/:action", function(req, res) {
var action = req.params.action;

if (action === "register" || action === "verify") {
SomeObject[action](req, res);
else {


It really depends on your use case, but if you've only got two options I'd recommend the first variant,

app.VERB("/email/register", register_handler)
app.VERB("/email/verify", verify_handler)

The second method is useful if you've got a large number of possible actions and they share similar handling code; for example, a custom API that you'll be calling through the frontend.