English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File(FS)

NodeJS Other

Node.js Redirect URL

Node.js Redirect URL: In this Node.js tutorial, we will learn about redirecting URLs.

Redirects can be applied in the following situations:

  • Some resources will be permanently moved to a new location, and you want to redirect the user to the new location of the moved resource.

  • Some pages in the web application have been deleted, and when the page is requested, you want to redirect the user to the homepage or some custom page.

There are mainly three types of HTTP redirects.

But remember, HTTP redirect codes (such as301,302,307etc.) can affect the page ranking of the original or redirected URL, and the impact of each redirect code is different. For example, if you permanently moved the resource, then in the response use301 The HTTP code will pass the juice to the redirect URL, while302or307it will not.

For the following example, consider two pages: page-a.html and page-b.html, which your web application serves. We have a404_find.html to display when the requested resource does not exist.

Node.js Redirect URL Example

In this example, we will demonstrate the scenario where the requested URL must be redirected. When we receive a request for page-When a request for c.html is made, we will send a redirect response to the web client (to find page-b.html).

var http = require('http'); 
var fs = require('fs'); 
 
// Create an http server
http.createServer(function(req, res) { 
    
    if (req.url == '/page-c.html') { 
        // Use in the response301(Permanent Move) Redirect HTTP code to page-b.html
        res.writeHead(301, { "Location": "http://" + req.headers['host'] + /page-b.html' }); 
        return res.end(); 
    } else { 
        // For other URLs, please try to respond with the page
        console.log(req.url) 
        // Read the requested file
        fs.readFile(req.url.substring1),}} 
            function(err, data) {  
                if (err) throw err; 
                res.writeHead(200)); 
                res.write(data.toString('utf8); 
                return res.end(); 
        ); 
    }  
 ).listen(8085);

Terminal Output

$ node node-js-http-redirect.js

Open the browser, display the developer tools, and then click the URL 'http:// localhost:8085/page-c.html.

In the 'Developer Tools' section 'Network', you will find that the request has been redirected to a new page.

For the first request, we sent from the Node.js application301Response code.

Node.js Redirect URL Example – File Not Found Error

In this example, we will demonstrate the case where the requested file is not found. However, you do not want to display boring404Error page. Instead, do you want to display other pages, such as page-a.html.

 var http = require('http'); 
var fs = require('fs'); 
 
// Create an http server
http.createServer(function(req, res) { 
    var filePath = req.url.substring(1); 
    fs.readFile(filePath, 
        function(err, data) {  
            // If an error occurs while reading the file, redirect it to page-b.html
            if (err) { 
                // Used when responding302 HTTP code redirect to page-b.html
                res.writeHead(302, { "Location": "http://" + req.headers['host'] + /page-b.html' }); 
                return res.end(); 
            } 
            res.writeHead(200)); 
            res.write(data.toString('utf8); 
            return res.end(); 
    ); 
 ).listen(8085);

Terminal Output

 
 $ node node-js-http-redirect-file-not-found.js

Open the browser, display the developer tools, and then click the URL 'http:// localhost:8085 / page-n.html.

In the 'Developer Tools' section 'Network', you will find that the request has been redirected to a new page, and the response contains302HTTP codes for (temporarily moved).