

The point is that Javascript return statement exits the request handler function (to prevent further code execution) and sends a response object back to the client. You'd be tempted to ask why the last server response has no return statement sending it to the client?, well in this case there is really no need to return it since there is no code further down the function to be excuted so it means the the request handler stops excuting since it has comee to the end of the road.

When a client makes a server request to this endpoint with or without a request body to the server request, the server sends the correct response and stop the function execution as necessary. What this means is that for a given client request the server previously sent a response (either a success responsei with the resource requested or error response for a bad request) back to the client and now is unexpectedly trying to send another response :(Ĭonst express = require ( ' express ' ) const bodyParser = require ( ' body-parser ' ) const app = express () app. SPOILER ALERT: I have run into this runtime error couple times while building restful API's and this is an effort to document what i learnt about this error, shorten your debugging time, help you understand why this error is thrown and finally how best handle it.Įrror is an interesting error that is fired up when a server tries to send more than one response to a client. Now you begin to ask yourself why you even choose this career path or even why you picked up node in the first place.

You must have written the perfect code and expect a flawless code excution (i wish 😆), but here is this runtime error shattering your expectations and definitely keeping you from moving on to other concerns of your project. : Cannot set headers after they are sent to the client Chances are as a NodeJS developer you've encountered this runtime error:
