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?
Asked
Active
Viewed 512 times
2

Milan Velebit
- 1,933
- 2
- 15
- 32
-
1I assume that this is some kind of lazy defensive programming (without explicit assert). Express runs on the server side, where it's considered more problematic to silently fails. So I guess the developers wanted a way to strongly show that something went wrong. Hence, just letting the closed socket crash by itself. – Swann Nov 02 '17 at 14:11
-
I guess [this answer](https://stackoverflow.com/questions/16180502/why-can-i-execute-code-after-res-send) gives appropriate explanation to this – kgangadhar Nov 02 '17 at 14:17
-
@kgangadhar Thanks for the response, but it doesn't really say exactly ***why*** they've opted for such a hardcore way to tell you that you're trying to bypass their paradigm. I've seen all related questions here. – Milan Velebit Nov 02 '17 at 14:20
-
@soueuls Yeah, seems to me so as well, if nobody posts an answer going into more detail, you should and I'll mark it as the correct one. I'll put on a bounty in two days just in case. – Milan Velebit Nov 02 '17 at 14:24
1 Answers
1
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.

Gabriel Bleu
- 9,703
- 2
- 30
- 43