node.jshttp-status-code-404http-status-code-200

different HTTP response types in node.js reflect same result


I have written the following create server code in node.js:

var http = require("http");

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end("Howdy");
}).listen(8081);

console.log("server running on port 8081"); 

This worked well and I got the response rendered on browser when run on localhost. Because HTTP status 200 means OK request (a successful HTTP request), i tried with 404, by editing line 4 to:

response.writeHead(404, {'Content-Type': 'text/plain'});

But I still got the same response on the browser as with http status 200. Because 404 shall send "Page not found" type of notification , I want to know if there is any problem in my code or http server configuration, or browser?


Solution

  • You are allowed to return content with a 404 response and the browser will display that content. Usually, it is some sort of message advising the end-user in a more friendly site-specific way that their page request was not found. You can see an example here: https://www.google.com/whatever or here http://www.cnn.com/whatever.

    You should NOT be using a 404 status unless the URL request really was not found or is not a valid request path. For example, a search engine will not index a page that returns 404.