Terminate ExpressJS request

Terminate ExpressJS request

Problem

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

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

Solution

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

Discussion

Leave a Reply

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