Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

express在中间件中使用app.get和route.get进行重定向的区别?

背景

使用中间件路由重定向

问题

在中间件中使用app.get('/', function(req,res){
res.redirect('/comic/index');
})和
app.get('/comic', function(req, res) => {
res.redirect('/comic/index');
}),重定向代码逻辑不会被执行

//中间件代码
module.exports = function redirectWm(app) {
   return function (req, res, next) {
       const path = req.path;
       // 重定向
       app.get('/', (req, res) => {
           // 不会被执行
           res.redirect('/comic/index');
       });
       app.get('/comic', (req, res) => {
           // 不会被执行
           res.redirect('/comic/index');
       });
       app.get('/ulink/comic', (req, res) => {
           // 可以正常执行
           res.redirect('/comic/ulink');
       });
       // 纯漫重定向
       if (path.indexOf('pure-comic') > -1) {
           res.redirect(path.replace('pure-comic', 'comic'));
       }
       next();
   }
}
// index.js
const express = require('express');
const history = require('connect-history-api-fallback');
const redirectFn = require('./middlewares/redirect');
const app = express();
module.exports = app;
app.use(redirectFn(app));
// 感觉像是connect-history-api-fallback影响了app.get的逻辑
app.use(history(
   {
       rewrites: [
           {//后缀为js|css 访问comic下相应文件
               from: /^/.*.(js|css|jpg|png|gif|ico|json|pdf|txt|mp3|mp4)$/,
               to: function(context) {
                   return context.parsedUrl.pathname;
               }
           },
           {//访问路径含comic则继续访问/comic/index.html
               from: /(^/comic/.*$)|(^/$)/,
               to: function(context) {
                   return '/comic/index.html';
               }
           },
           {//访问路径不含comic则默认访问
               from: /^/.*$/,
               to: function(context) {
                   return context.parsedUrl.pathname;
               }
           },
       ]
   }
));

在中间件中使用router.get('/', function(req,res){
res.redirect('/comic/index');
})和
router.get('/comic', function(req, res) => {
res.redirect('/comic/index');
}),重定向代码逻辑可以正常执行

// 中间件代码
module.exports = function redirectWm(router) {
   return function (req, res, next) {
       const path = req.path;
       // 重定向
       router.get('/', (req, res) => {
           res.redirect('/comic/index');
       });
       router.get('/comic', (req, res) => {

           res.redirect('/comic/index');
       });
       router.get('/ulink/comic', (req, res) => {
           res.redirect('/comic/ulink');
       });
       // 纯漫重定向
       if (path.indexOf('pure-comic') > -1) {
           res.redirect(path.replace('pure-comic', 'comic'));
       }
       next();
   }
}
// index.js
// index.js
const express = require('express');
const history = require('connect-history-api-fallback');
const redirectFn = require('./middlewares/redirect');
const app = express();
const router = express.Router();

module.exports = app;
app.use('/', router);
app.use(redirectFn(router));

app.use(history(
   {
       rewrites: [
           {//后缀为js|css 访问comic下相应文件
               from: /^/.*.(js|css|jpg|png|gif|ico|json|pdf|txt|mp3|mp4)$/,
               to: function(context) {
                   return context.parsedUrl.pathname;
               }
           },
           {//访问路径含comic则继续访问/comic/index.html
               from: /(^/comic/.*$)|(^/$)/,
               to: function(context) {
                   return '/comic/index.html';
               }
           },
           {//访问路径不含comic则默认访问
               from: /^/.*$/,
               to: function(context) {
                   return context.parsedUrl.pathname;
               }
           },
       ]
   }
));

而如果直接在index.js中执行app.get('/',callback),也是能正常执行回调逻辑的,所以不太清楚在中间件中app和router有什么区别导致了上述的差异,connect-history-api-fallback中间件是否对以上的逻辑有影响?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

有个很大的问题,app.use(redirectFn(router))app.use(redirectFn(app)) 用法不对,redirectFn 里面有注册路由,每次访问,redirectFn里的注册逻辑每次都执行一次

两种写法不一致,很可能和 middlewre 注册顺序有关


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...