Express JS Node JS

How to make 301 Redirect with Node.js / Express.js

301 redirects node express
Written by VithalReddy.M.B

Node.js is built on Chrome’s V8 JavaScript runtime for building super fast and scalable web applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across all devices.This all properties make it a wonderful choice for your next project and Sometimes you need to redirect your old project url to new with 301 Moved Permanently redirect learn more. So in this tutorial, we will learn to make single or multiple 301 redirects with node.js / express.js with an example.

we will discuss 301 redirects using nodejs / express and 301 redirects using nodejs only. So you can decide which method to use based on your project requirements.

How to  301 redirects using Node.js – tutorial

We are going to use Node.js HTTP module to 301 redirects.

  • require HTTP module using.const http = require('http');
  • create a server using method http.createServer().
  • write header status 301 using res.writeHead() function.
  • define new domain inside res.writeHead();
  • define the server.listen port and you’ve successfully created your 301 redirects in node.

Full code:

const http = require('http');
//creating server using nodejs http module
const server = http.createServer((req, res, next) => {
  //replace stackfame.com with your doamin name
  res.writeHead(301, {'Location' : 'http://www.stackfame.com'});
  res.end();
});

server.listen(process.env.PORT || 3000);

That was really easy. isn’t it? yes if have any queries, please comment below.

READ  Google Recaptcha Node.JS (Express.js) Tutorial

How to  301 redirects using Express.js / Node.js – tutorial

Now Let’s see how to redirect your old domain name shiny new domain using one of most downloaded npm module express.js.

  • install express using npm install express --save
  • define express app using app = express();
  • using get request and express’s res.redirect() method do the 301 redirect

Full code:

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


app.get('/old-url/',  (req, res, next) => {  
    res.redirect(301, 'https://new-doamin.com/new-url');
});

app.listen(port, () => {
    console.log('Server is running at port: ',port);
});

and if you want force live request using express middleware use following code:

// forced 301 redirect using express middleware 
const express = require('express');
const  app = express();
const port = 3000;

app.use((req, res, next) => {
  
  var host = req.get('Host');
  if (host === 'www.old-domain.com') {
    return res.redirect(301, 'https://new-domain.com/' + req.originalUrl);
  }
  return next();
});

app.listen(port, () => {
    console.log('Server is running at port: ',port);
});

Congrats folks, you’ve successfully redirected your old URL to new with 301 redirect using node/express and if you have any queries please comment below about node js redirect domain or express redirect to URL.

Conclusion:

No matter if you used express or nodejs, you will get same superfast results and don’t forget to add next in get request, otherwise your nodejs app can stop working or hang sometimes. so use next in get request.You can also implement this to nodejs redirect to another URL and internal URL and vice-versa.

About the author

VithalReddy.M.B

Vithalreddy.M.B is the Editor of VMRworld.com & stackFame.com with a focus on startups, Startup News, Business, and Technology.He is also an Electrical & Electronics Engineer, Technology Lover, and Entrepreneur by choice.Started His first startup at 21.