Terminate ExpressJS request

Terminate ExpressJS request


How would I end an Express 3 request? res.end() certainly doesn’t stop the processing.

exports.home = function(req, res) {
  console.log('Still going, going...');
Problem courtesy of: Yashua


You’ll need to return. E.g.,

exports.home = function(req, res) {
  return res.end();
  console.log('I will never run...');

res.end() simply completes and flushes the response to the client. Just like any other action, however, that doesn’t tell JavaScript to stop running so we need to explicitly return out of the function (although I might ask the question of why you would have code after flushing the response that you don’t actually want to run…?).

Solution courtesy of: jmar777


Leave a Reply

Your email address will not be published. Required fields are marked *