Open In App

How to redirect back to original URL in Node.js ?

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js with the help of Express, supports web page routing. This means that when the client makes different requests, the application is routed to different web pages depending upon the request made and the routing methods defined. To learn more about Node.js routing and its implementation, refer this article.

In this article, we will discuss how to redirect back to the original URL in Node.js.

The res.redirect() is a URL utility function that helps to redirect the web pages according to the specified paths.

Syntax:

return res.redirect([status], path)

Example: For the first example, we will redirect the user to a specified URL with a different domain. Make sure to install express in your project before running the code.

Javascript




const express = require('express');
  
const app = express();
  
app.get('/', function (req, res) {
    // On getting the home route request,
    // the user will be redirected to GFG website
    res.redirect('https://www.geeksforgeeks.org');
});
  
app.listen(3000, function (req, res) {
    console.log("Server is running at port 3000");
});


When the above code is executed using node, we will be redirected to the GeeksforGeeks website when we make a request for the home route on port 3000.

Output:

Other methods: Other than redirecting to a different domain, we have some other methods available to redirect as follows.

Domain relative redirect: We can use this method to redirect to a different page under the same domain. For example, if the user is at http://example.com/gfg/post1 , then we can redirect to http://example.com/article by using the following line of code

res.redirect('/article');

Pathname relative redirect: We can use this method to redirect to the previous path on the website. For example, if the user is at http://example.com/gfg/post1, then we can redirect to http://example.com/gfg by using the following line of code

res.redirect('..');

Redirecting back to the original URL: 

After learning about res.redirect() function, we can now discuss how to redirect back to the original URL in NodeJS.

Back redirect:

We can use this method to redirect the request back to the referrer. If no referrer is present, the request is redirected to the “/” route by default.

res.redirect('back');

Example:

Let’s suppose we are at ‘/example’ route. On requesting the ‘/redex’ route, we will be automatically redirected to ‘/’ route as defined in the code below

Javascript




const express = require('express');
  
const app = express();
  
//Defining '/' route
app.get('/', function (req, res) {
    res.redirect('https://www.geeksforgeeks.org');
});
  
//Defining '/example' route
app.get('/example', function (req, res) {
});
  
//Defining '/redex' route
app.get('/redex', function (req, res) {
    res.redirect('back');
})
  
app.listen(3000, function (req, res) {
    console.log("Server is running at port 3000");
});


Output: 

Reference:https://expressjs.com/en/5x/api.html#res.redirect



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads