In Node Js route.js when user try to open
app.use('/demo1', require('express').static(__dirname + '/demo1'));
then i want to redirect to
app.use('/demo2', require('express').static(__dirname + '/demo2'));
like In the browser user type http://locahhost:5010/demo1 this URL
but it will open http://locahhost:5010/demo2 this URL
In Node Js route.js when user try to open
app.use('/demo1', require('express').static(__dirname + '/demo1'));
then i want to redirect to
app.use('/demo2', require('express').static(__dirname + '/demo2'));
like In the browser user type http://locahhost:5010/demo1 this URL
but it will open http://locahhost:5010/demo2 this URL
Share Improve this question edited Dec 12, 2016 at 6:09 Sudhansu Choudhary 3,3603 gold badges20 silver badges32 bronze badges asked Dec 12, 2016 at 6:04 AnupamAnupam 2012 gold badges4 silver badges12 bronze badges4 Answers
Reset to default 20Use redirect in your route Express redirect
Example:
app.get('/demo1', function(req, res) {
res.redirect('/demo2');
});
app.use((req, res, next) => {
if (req.url == '/') {
res.redirect('/en');
return;
}
next();
})
I was thru the same and i had to use return next() was calling before
You probably created a route. To fix, change the about router from this:
app.get('/demo1', function(req, res) {
res.redirect('/demo2');
});
You can try this is as well if you have routes in seperate files. Let say we have it in demo1route.js in the route folder.
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('/demo2');
});
and then use below code in app.js
var demo1 = app.require('../demo1route.js')
app.use('/demo1',demo1);
app.get('/demo1', function(req, res) {
res.redirect('/demo2');
});
this should work fine if /demo2 is a get request router