express redirect to another route code example

Example 1: javascript redirect

window.location.href = "http://mywebsite.com/home.html";

Example 2: express redirect to url

app.get('/', (req, res) => {
  res.redirect('/about');
})

Example 3: express redirect

res.redirect('/foo/bar')
res.redirect('http://example.com')
res.redirect(301, 'http://example.com')
res.redirect('../login')

Example 4: express js redirect to url

app.all('/secret', function (req, res, next) {
  console.log('Accessing the secret section ...')
  next() // pass control to the next handler
})

Example 5: express js redirect to url

var express = require('express')
var app = express()

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
  res.send('hello world')
})

Tags:

Misc Example