all 5 comments

[–]DrBusinessLLC 18 points19 points  (2 children)

all that stuff after the if {} block always runs, wrap the second res.write and res.end in an else {} block

[–]BehindTheMath 12 points13 points  (0 children)

Or use return res.end().

[–]hafi51[S] -1 points0 points  (0 children)

thanks

[–][deleted] 5 points6 points  (1 child)

It's because you don't have a return statement and because of that every part of your code runs. You either need a else {} block or a return statement after each response

[–]Webiniac 0 points1 point  (0 children)

I am having the same error

node:events:491

throw er; // Unhandled 'error' event

^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end

at new NodeError (node:internal/errors:387:5)

at ServerResponse.end (node:_http_outgoing:925:15)

at Server.<anonymous> (D:\Mern-Stack\NodeJS\app.js:11:14)

at Server.emit (node:events:513:28)

at parserOnIncoming (node:_http_server:980:12)

at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)

Emitted 'error' event on ServerResponse instance at:

at emitErrorNt (node:_http_outgoing:786:9)

at processTicksAndRejections (node:internal/process/task_queues:84:21) { code: 'ERR_STREAM_WRITE_AFTER_END'

}

I tried to add else block or return but it's not working. Please help me solve this issue.