Categories
JavaScript Answers

How to redirect user’s browser URL to a different page in Node.js?

Spread the love

To redirect user’s browser URL to a different page in Node.js, we call the Express res.redirect method.

For instance, we write

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

app.get("*", (req, res) => {
  res.redirect("https://www.example.com/");
});

app.set("port", process.env.PORT || 3000);
const server = app.listen(app.get("port"), () => {});

to call res.redirect to redirect to https://www.example.com in the catch get route we created.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *