Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 01 Apr, 2021

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 the implementation, refer this article.

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

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

Syntax:

return res.redirect([status], path)

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');
  
var 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 request for home route on port 3000.

Output:

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

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 original URL:



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

Back redirect:

We can use this method to redirects the request back to the referer. If no referer is present, the request is redirected to “/” 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');
  
var 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :