Receiving Custom error messages from fetch

Couldn’t receive custom error messages that is responded by express server

when I use fetch, however jQuery.ajax.fail() could receive it

http://expressjs.com/en/api.html#res.send
http://expressjs.com/en/api.html#res.json

Still I get the default statusText in response

For example,
res.status(400).send({ message: ‘Password reset token is invalid or has expired’ });
I get statusText=Bad request and I don’t receive message

Author: Fantashit

1 thought on “Receiving Custom error messages from fetch

  1. Express does not provide an API for this because not all of the supported Node.js versions of Express support this functionality, and it’s not recommended you do this, because your user may have a proxy between them and your server that assumes the status message is not custom, breaking your expectations in the browser.

    If you really want to do this, you’ll need to use the raw Node.js directly: https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_response_statusmessage and be using Node.js 0.12+ and have no intermediate proxies between your Node.js server and the browser that rewrite that line (it’s very common for that to happen, which is another reason we don’t even bother to offer an Express API).

    You use Node.js APIs in Express directly:

    app.use(function (req, res) {
      res.status(400)
      res.statusMessage ='Nothing'
      res.send({ message: 'I have nothing for you' })
    })

Comments are closed.