When you don't want to send back a response

What is the best pattern for closing an express function when you don’t want to create a response?

model.save(function (err){
  if (err) {console.log(err);}
});

is it ok just to ignore the returned data?

You still need to send a response. If you don’t want the response to have any data, use response.end(). You might also want to send an appropriate status code, like response.status(204).end(). 204 might not be appropriate in your case though.

1 Like

thanks @kevcomedia

I have been using those res.status(404).json({ message: "Cannot find project" })
But the status 404 seems to stop the message being sent back to the front end

Your frontend should still receive a response, even if it’s a 404. You can check your browser’s Network tab to see.

However some ajax libraries (like axios) need that you define a separate handler for responses outside the 2xx range.

(Or I probably misunderstood your last sentence)

1 Like