152

How do I get a node.js server to redirect users to a 404.html page when they enter an invalid url?

I did some searching, and it looks like most results are for Express, but I want to write my server in pure node.js.

2
  • 1
    I used to believe that my app was better in pure node.js, but have been VERY happy after installing express. I can't imagine a node SPA/RESTful web service architecture without express now.
    – Graham
    Aug 16, 2016 at 0:44
  • @Graham I had a question (What can standalone Express do?) to learn from precisely this type of experience you mention, but it was deleted. If you explain the extra functionality or benefits you gained from Express somewhere else, I'd be very interested to read it.
    – Nagev
    Jun 2, 2022 at 10:35

9 Answers 9

243

The logic of determining a "wrong" url is specific to your application. It could be a simple file not found error or something else if you are doing a RESTful app. Once you've figured that out, sending a redirect is as simple as:

response.writeHead(302, {
  'Location': 'your/404/path.html'
  //add other headers here...
});
response.end();
4
  • it doesn't show my 404 html page, maybe it's just show the header. Oct 31, 2010 at 8:10
  • 8
    @Magic Not true. Try Location: / and it would redirect you to the root folder. Oct 24, 2014 at 5:35
  • Agreed with Awal. I'm using 'Location': '/path/to/my/servers/content' May 1, 2015 at 5:17
  • 1
    Thank you. I am running a static server and somehow installed Node without admin privileges. I don't have admin privileges on the PC, so I can't change the setup. Anyway, I managed to install Node.js without NPM or anything but the default packages, so I've been working hard to do everything using vanilla Node.js. Your technique let me set a dynamic port 80 forwarding script on my private IP to redirect all port 80 HTTP requests to whatever the port for the project I'm working on is. Thank you. Now, I'm just considering forwarding them to port 80 just for the fun of it. 🙂
    – 9pfs
    Mar 27, 2021 at 20:50
88

If you are using ExpressJS, it's possible to use:

res.redirect('your/404/path.html');
4
  • 91
    In express only. Not in native node.js app. Dec 3, 2014 at 17:15
  • 2
    @TarunG Thanks for the comment, if you look at the edit history of the question you see that this wasn't always so clear. Sep 6, 2015 at 20:42
  • 3
    In my case res.redirect('/login') merely sends the login.html content in the response of the ajax call made. How do I rather open the page?
    – Tarun
    Oct 24, 2016 at 20:03
  • Old post, but maybe you are looking for something like res.direct('localhost:5000/login'), especially if you're running your express server on a different port than your frontend like I happen to be.
    – jboxxx
    Oct 20, 2019 at 19:46
18

To indicate a missing file/resource and serve a 404 page, you need not redirect. In the same request you must generate the response with the status code set to 404 and the content of your 404 HTML page as response body. Here is the sample code to demonstrate this in Node.js.

var http = require('http'),
    fs = require('fs'),
    util = require('util'),
    url = require('url');

var server = http.createServer(function(req, res) {
    if(url.parse(req.url).pathname == '/') {
        res.writeHead(200, {'content-type': 'text/html'});
        var rs = fs.createReadStream('index.html');
        util.pump(rs, res);
    } else {
        res.writeHead(404, {'content-type': 'text/html'});
        var rs = fs.createReadStream('404.html');
        util.pump(rs, res);
    }
});

server.listen(8080);
16

404 with Content/Body

res.writeHead(404, {'Content-Type': 'text/plain'});                    // <- redirect
res.write("Looked everywhere, but couldn't find that page at all!\n"); // <- content!
res.end();                                                             // that's all!

Redirect to Https

res.writeHead(302, {'Location': 'https://example.com' + req.url});
res.end();

Just consider where you use this (e.g. only for http request), so you don't get endless redirects ;-)

6

I used a switch statement, with the default as a 404:

var fs = require("fs");
var http = require("http");

function send404Response (response){
    response.writeHead(404, {"Content-Type": "text/html"});
    fs.createReadStream("./path/to/404.html").pipe(response);
}

function onRequest (request, response){
    switch (request.url){
        case "/page1":
            //statements
            break;
        case "/page2":
            //statements
            break;
        default:
        //if no 'match' is found
            send404Response(response);
            break;
    }
}

http.createServer(onRequest).listen(8080);
5

Try this:

this.statusCode = 302;
this.setHeader('Location', '/url/to/redirect');
this.end();
3
  • 1
    This is probably an Express code snippet OP asked for pure node.js code Oct 11, 2016 at 17:44
  • 1
    It's not Express code. This looks like vanilla Node code except for the this part. In NodeJS you write a handler that accepts two parameters (request, response) and all those calls in the answer are on the response object. Nov 12, 2018 at 20:09
  • But why did you just post the same information as the accepted answer from 2010? Oct 21, 2023 at 17:16
2

You have to use the following code:

response.writeHead(302 , {
           'Location' : '/view/index.html' // This is your url which you want
        });
response.end();
1
  • But why did you just post the same information as the accepted answer from 2010? Oct 21, 2023 at 17:15
2

Use the following code this works fine in Native Nodejs

http.createServer(function (req, res) {
var q = url.parse(req.url, true);
if (q.pathname === '/') {
  //Home page code
} else if (q.pathname === '/redirect-to-google') {
  res.writeHead(301, { "Location": "http://google.com/" });
  return res.end();
} else if (q.pathname === '/redirect-to-interal-page') {
  res.writeHead(301, { "Location": "/path/within/site" });
  return res.end();
} else {
    //404 page code
}
res.end();
}).listen(8080);
-2

use

res.redirect('/path/404.html');

or you can redirect to any defined URI as

res.redirect('/');
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.