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

14 Jan, 2022
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 referrer. If no referrer 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 '/' routeapp.get('/',function(req,res){  res.redirect('https://www.geeksforgeeks.org');}); //Defining '/example' routeapp.get('/example',function(req,res){  res.redirect('https://www.geeksforgeeks.org/array-data-structure/');}); //Defining '/redex' routeapp.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
adnanirshad158
NodeJS-Questions
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between dependencies, devDependencies and peerDependencies
Node.js Export Module
How to connect Node.js with React.js ?
Mongoose find() Function
Mongoose Populate() Method
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript