.sendStatus(500)

In the solution code for app.js in the Codestrips project (you can download it as a zip file at the bottom of the linked page), I repeatedly see the following line of code for the cases where there is an error with regards to working on the database:

return res.sendStatus(500);

I have two questions on this.

(1) In the CodeCademy’s Express.js course, we have only learned res.status(500).send(). What’s the difference?

(2) Should the return keyword be necessary? In the solution code, in one place, it’s like

app.get('/strips', (req, res, next) => {
  db.all('SELECT * FROM Strip', (err, rows) => {
    if (err) {
      res.sendStatus(500);
    } else {
      res.send({strips: rows});
    }
  });
});

while in other places

    db.get(`SELECT * FROM Strip WHERE id = ${this.lastID}`, (err, row) => {
      if (!row) {
        return res.sendStatus(500);
      }
      res.status(201).send({strip: row});
    });

When do we need return?

2 Likes

res.sendStatus(statusCode) is a shortcut. Quote from the documentation:

Sets the response HTTP status code to statusCode and send its string representation as the response body.

res.sendStatus(200) // equivalent to res.status(200).send('OK')
res.sendStatus(403) // equivalent to res.status(403).send('Forbidden')
res.sendStatus(404) // equivalent to res.status(404).send('Not Found')
res.sendStatus(500) // equivalent to res.status(500).send('Internal Server Error')

So if you don’t want to customize the message that will be sent back - use sendStatus. res.status(statusCode).send(message) allows you to specify the custom message.


return terminates execution of the function. So if we would remove return from the second example then if !row would result in true then the line res.status(201).send({strip: row}); would also be executed. The response was already sent, so it does not change anything for the client of the API, but it simply does not make sense to execute res.send... multiple times.

I prefer to use return in route controllers. It makes code more readable.

4 Likes

Thank you, @factoradic, for your response!

One question in response to what you wrote:

I prefer to use return in route controllers. It makes code more readable.

Am I correct to understand that using return is more readable because we do not need else {} which could end up nesting logic like this?

if (...) {
  ...
} else {
  if (...) {
    ...
  } else {
    if (...) {
      ...
    } else {
      ...
    }
  }
}

And the same code can be written with return like this:

if (...) {
  return
}
if (...) {
  return
}
if (...) {
  return
}
...
1 Like

Yes, that’s exactly what I meant :slight_smile:

There is also one more aspect:

if (...) {
  ...
} else {
  if (...) {
    ...
  } else {
    if (...) {
      ...
    } else {
      ...
    }
  }
}

// even more lines of code that will be executed no matter what

Imagine that there is this huge route controller that was written by some junior programmers. You have to analyze what happens when body of the POST request is empty. So you open the code for the route contoller function and you see that in the first line there is an if checking if the POST body has all the required values. If not - res.sendStatus(400) is executed, without a return keyword.

This is not the end of your task, now you have to scroll down to the end of this massive if else contraption to check if there are any lines of code below it. And sure, there are keyboard shortcuts that allow you to jump to the matching ending bracket etc. But it will always require the time and some cognitive power.

return res.sendStatus(400) solves this problem. You know that this is the end of the execution.