How to redirect with express

In this article, you will learn about how to redirect with express. Express is a popular backend web application framework for nodejs. It is mainly…

The post How to redirect with express appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven

In this article, you will learn about how to redirect with express.

Express is a popular backend web application framework for nodejs. It is mainly designed for building web applications and APIs. Express lets you write server-side logic for web and mobile applications. You can easily redirect a URL in express to do so you have to use res.redirect() function which is provided by express itself.

This res.redirect() function accepts two parameters one is status which refers to HTTP status code and another one is PATH which describes the PATH you want to redirect in. Let’s see an example:

const express = require('express');
const app = express();

app.get('/', function(req, res){
    res.redirect('/registration'); // Redirected to registration Page
});
  
app.get('/registration', function(req, res){
    res.send("Redirected to registration Page");
});
 
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`App is running on port ${PORT}...`);
});

You can also set the status code in the redirect function. By default, the status code is 302 which means FOUND. But you can set the status code explicitly. Let’s see an example with the status code.

const express = require('express');
const app = express();

app.get('/', function(req, res){
    res.redirect(301,'/registration'); // Redirected to registration Page
});

app.get('/registration', function(req, res){
    res.send("Redirected to registration Page");
});
 
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`App is running on port ${PORT}...`);
});

Here, the first parameter expects a numeric value and the status code 301 indicates that the URL has permanently changed.

This is all about redirect with express and you may learn how to redirect with express by using res.redirect().

The post How to redirect with express appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-12-01T05:43:13+00:00) How to redirect with express. Retrieved from https://www.scien.cx/2021/12/01/how-to-redirect-with-express/

MLA
" » How to redirect with express." Deven | Sciencx - Wednesday December 1, 2021, https://www.scien.cx/2021/12/01/how-to-redirect-with-express/
HARVARD
Deven | Sciencx Wednesday December 1, 2021 » How to redirect with express., viewed ,<https://www.scien.cx/2021/12/01/how-to-redirect-with-express/>
VANCOUVER
Deven | Sciencx - » How to redirect with express. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/01/how-to-redirect-with-express/
CHICAGO
" » How to redirect with express." Deven | Sciencx - Accessed . https://www.scien.cx/2021/12/01/how-to-redirect-with-express/
IEEE
" » How to redirect with express." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/12/01/how-to-redirect-with-express/. [Accessed: ]
rf:citation
» How to redirect with express | Deven | Sciencx | https://www.scien.cx/2021/12/01/how-to-redirect-with-express/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.