In this tutorial we will see how to do a redirect using Express JS. Typically an http redirect is used when we want to redirect the url to a different page by keeping an existing url. Express JS achieves the redirect by using the function redirect() which is part of the response object. Let us take a look at few examples.

Express JS Redirect – Example

A redirect can be to an internal path or to an external website as well. In the below example, first we will create an http GET service and do a redirect to the same. We will also have a redirect to

var express = require("express");
var app = express();
//GET Method

//Redirect to /hello

//Redirect to external site.

Run the above code and make a call to http://localhost:8080/redirect and you will see it redirects to http://localhost:8080/hello and return the response. Now, make a call to http://localhost:8080/redirect_ext and it will redirect to By default the redirect uses http status code 302. The redirect function also takes an optional parameter to set the status code like below. In the below code we are setting the redirect status code to 301.

	res.redirect(301, "./hello");
How to redirect in Express JS?