Redirect within Route

How do I redirect within a route?

var express = require('express');
var router = express.Router();

router.get('/', function(req,res){
    //do something
});

router.get('/delete/:id', function(req, res){
   // redirect to / in current route??
});

Author: Fantashit

1 thought on “Redirect within Route

  1. @tarlabs yes, you just want to examine/concat to req.baseUrl. For your example above:

    var express = require('express');
    var router = express.Router();
    
    router.get('/', function(req,res){
       //do something
    });
    
    router.get('/delete/:id', function(req, res){
      // redirect to / in current router
      res.redirect(req.baseUrl + '/');
    });

Comments are closed.