admin管理员组

文章数量:1302319

The question is simple: Does returning a value in the request handler of an express route do anything?


I've seen people return the response of the request handler:

route.get('/something', (req, res) => {
  // blah blah

  if (/* something */) {
    return res.send('something'); // return on the same line
  }

  return res.send('something else');
});

and I've seen people do an early return after sending a response:

route.get('/something', (req, res) => {
  if (/* something */) {
    res.send('something');
    return; // return after
  }

  res.send('something else');
  return;
});

Is there any difference? Does the returned value get used at all?

The question is simple: Does returning a value in the request handler of an express route do anything?


I've seen people return the response of the request handler:

route.get('/something', (req, res) => {
  // blah blah

  if (/* something */) {
    return res.send('something'); // return on the same line
  }

  return res.send('something else');
});

and I've seen people do an early return after sending a response:

route.get('/something', (req, res) => {
  if (/* something */) {
    res.send('something');
    return; // return after
  }

  res.send('something else');
  return;
});

Is there any difference? Does the returned value get used at all?

Share Improve this question asked Aug 28, 2017 at 14:13 Rico KahlerRico Kahler 19.3k13 gold badges67 silver badges86 bronze badges 2
  • I think one can return a string that is passed to the response ( like res.write()), however in this case theres no difference afaik. – Jonas Wilms Commented Aug 28, 2017 at 14:21
  • 2 There is no difference. Both aim for an early exit. I would prefer going with the separate return statement, to avoid the impression that the return value is significant. Sometimes you'll also find return console.log('error occurred', err). It is a misleading shortcut. – trincot Commented Aug 28, 2017 at 14:30
Add a ment  | 

2 Answers 2

Reset to default 10

Does returning a value in the request handler of an express route do anything?

No it does not. Express does not pay any attention to whatever value you return from your route handler.

When you see something like:

return res.send(...);

that is really just the same in Express as:

res.send(...);
return;

There is no difference in what happens in Express. The return is used in this circumstance as flow control only (to exit the function), not because someone wants to return a value so either of these has the exact same behavior.

I personally prefer the second form because the first form can be interpreted by someone reading the code to imply there might be meaning to a returned value there (even though there isn't).

To add to the above a return value could be useful for unit testing. You could return a value which could be checked in a test framework and avoid having to use something like supertest for basic testing of a module or class.

本文标签: javascriptDoes returning a value in the request handler of an express route do anythingStack Overflow