Express server Error 400 is not returning json message


I am submitting a form and if the form id already exists in the database, I am returning status 400 with a message saying that the form exists.

   status: 400,
   message: "Form exists"

When I read the response sent back from my express server, I am getting Bad request message instead of the custom message object that I am returning. If I replace res.status(400) with res.status(200), I am getting the expected custom message object.

Weird enough, I can get the custom message object when making the server call in development environment. I get Bad Request message from my production server and I don’t know why the response is different when the environment is different. I am hosting this server on IIS Manager v10

So my question is should I use status code of 200 instead of 400 in this scenario? Is there a way to return a custom message from status 400? Based on my understanding, I should use 4xx status code if there is a client input errors eg there is already an existing ID or invalid inputs.

Edit: This is my code from my React app.

        .post("/form/some-endpoint", formData)
        .then(function () {
        .catch(function (error) {
          // eslint-disable-next-line no-console
          alert(error.response !== undefined ? : error.message);

This is the actual screenshot of the response from prod server (I console log it)

enter image description here

enter image description here

But in development environment, I am getting the response that I wanted.
enter image description here

enter image description here

Postman response from Prod server:
enter image description here


    <httpErrors existingResponse="PassThrough" />

Adding the <httpErrors existingResponse="PassThrough" /> to the server’s web.config file on IIS Manager resolved my issue. Based on my understanding, bypassing the http error handler and not letting IIS to send its response is the solution that I need.

Answered By – calvert

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published