0

I have set up a NodeJS server, listening on localhost:3000, and I am testing it with Chrome browser.

When the response status code is 500:

  • If the response body is empty, then the client shows:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
  • If the response body is not empty, then the client (surprisingly) shows it with no error

Here is a sample code to reproduce this:

let http = require("http");

let state = true;

let server = http.createServer(async function(request, response) {
    if (request.url == "/") {
        response.statusCode = 500;
        response.end(state ? "" : "data");
        console.log(state);
        state = !state;
    }
});

server.listen(3000, async function(error) {
    if (error)
        return console.log(error);
    console.log("server is listening");
});

To tell you the truth, I am not worried about how the browser displays it, because my real client is some other process in the system. But I want to make sure that this process receives the correct status code (500) even if I enclose some data in the response body.

So I'd like to know if there is something wrong in my code (i.e., if I am not allowed to send data along with status 500), or if it's just Chrome's default behavior.

halfer
  • 19,824
  • 17
  • 99
  • 186
goodvibration
  • 5,980
  • 4
  • 28
  • 61

1 Answers1

3

According to this post and this discussion Chrome if displaying a "friendly" error page is server responded with error code without the body (earlier it was for less than 512 bytes in response) - that's why you see a different behavior.

In most cases you would send the custom error page along with the error code, to display it to user. So there's no need to show error if there's a custom page displaying it anyway.

And if you just need to validate the response headers there are special extensions to chrome that allows you to see it. For example there's HTTP Headers. Also I suppose you could use some kind of debugging proxy, like for example fiddler to capture the traffic, and inspect the requests and responses.

Konrad 'Zegis'
  • 15,101
  • 1
  • 16
  • 18