Skip to content
Advertisement

Write after end error when launch server in nodejs

I am beginner at NodeJS and I’m doing a “NodeJS and Express.js full course” at freecodecamp yt and I copied author code which for him works perfectly, but I got an error.

Code:

JavaScript

I don’t know why he got home, about and error page when user goes to the wrong page it should throw “Error” text on page, but instead my program is throwing an error in nodeJS program:

JavaScript

Can someone explain this to me? I would be appreciate. Thank you in advance.

Advertisement

Answer

That’s caused by the res.end('Error'), which gets always executed. Try to put it into an else clause, or put a return before each res.end(…)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement