Milan Velebit
Milan Velebit

Reputation: 2027

Why does sending multiple responses per one request crash a NodeJS server?

Might be an idiotic question, but I was wondering why does, i.e. invoking Express' res.send() (a subclass of NodeJS' http.ServerResponse) more than once per a single request shut down a NodeJS server? Why doesn't it end the request while sending the first response and simply log the error, without crashing?

Upvotes: 2

Views: 546

Answers (1)

Gabriel Bleu
Gabriel Bleu

Reputation: 10204

Express is just throwing an exception, then node handles it :

The 'uncaughtException' event is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. By default, Node.js handles such exceptions by printing the stack trace to stderr and exiting. doc

If you want to do something else, implement your own process.on('uncaughtException', (err) => {})

Or you could let it crash and use stuff like forever to bring it back up.

Upvotes: 1

Related Questions