gpt4 book ai didi

javascript - 表达 js 流行为

转载 作者:行者123 更新时间:2023-12-03 04:29:33 28 4
gpt4 key购买 nike

我需要了解使用路由在 Express 应用程序中流程的工作原理,我有这些路线

app.use(require('./routes/reportsRouter'));
app.use(require('./routes/crewsRouter'));
app.use(require('./routes/api'));
app.use(require('./routes/filesRouter'));

现在在 ./routes/crewsRouter 中我有以下代码

var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {

var url = req.url;
//..... Edit URL if Contains // empty parm
// crews//today; correct Url crews/all/today
// this give me a list of all jobs for all crews for today.
console.log("CrewsRouter: ", req.method + ".( " + url + " )");
next();
});
router.get('/crews', function(req, res) {
if (!req.params.key) { next(); }
res.render('crewsView',{
pageTitle:'All-Crews',
pageID:'crews',
crewInfo: {"aka": "all"},
reqOptions: ''
});

});
router.get('/crews/:leadId?/:options?', function(req, res) {....}
module.exports = router;

和 reportsRou​​ter.js

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

router.use(function(req, res, next) {

// log each request to the console
console.log("ReportsRouter: ", req.method + ".( " + req.url + " )");
// continue doing what we were doing and go to the route
next();
});

router.get('/reports', function(req, res) {
//var data = req.app.get('appData')

res.render('reportsView',{
pageTitle:'Reports',
pageID:'reports'
});
});
module.exports = router;

无论我请求什么路线,我的行为都是如此两个 route.use 都在运行。这是正常现象吗?我可以采取什么措施来阻止这种行为。

最佳答案

let crewsRouter = require('routes/crewsRouter');
...

app.use('/crews', crewsRouter);
app.use('/reports', reportsRouter);

# 工作人员

...
router.get('/', function(req, res) {
... # this used to be your '/crews' handler
}

# 份报告

...
router.get('/', function(req, res) {
... # this used to be your '/reports' handler
}

关于javascript - 表达 js 流行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43547372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com