最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redirect to another path in Node JS route - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a comment  | 

4 Answers 4

Reset to default 20

Use 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

发布评论

评论列表(0)

  1. 暂无评论