Express 快速路线中的可变参数
在ExpressJS中,有没有一种使用变量作为第一个参数的路由的方法 我试着做一些类似的事情:Express 快速路线中的可变参数,express,routing,Express,Routing,在ExpressJS中,有没有一种使用变量作为第一个参数的路由的方法 我试着做一些类似的事情: app.use('/:customer/data',组件) 然后在组件中执行以下操作: router.get('/:customer/manifest',函数(req,res){…} 如果我这样做的话,我就无法到达我的路线,我想知道是否有其他选择 下面是我的代码当前的示例: app.js The setup before this is working correctly, so suppose we
app.use('/:customer/data',组件)代码>
然后在组件中执行以下操作:
router.get('/:customer/manifest',函数(req,res){…}
如果我这样做的话,我就无法到达我的路线,我想知道是否有其他选择
下面是我的代码当前的示例:
app.js
The setup before this is working correctly, so suppose we only zoom in on the route itself
// Manifest route to retrieve customer context manifest
app.use('/:customer/manifest', manifest);
router.get('/:customer/manifest', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
app.use('/:customer/manifest', manifest);
router.get('/', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
manifest.js
The setup before this is working correctly, so suppose we only zoom in on the route itself
// Manifest route to retrieve customer context manifest
app.use('/:customer/manifest', manifest);
router.get('/:customer/manifest', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
app.use('/:customer/manifest', manifest);
router.get('/', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
我希望我的路径类似于/{customer}/manifest
,并且它指向正确的路径。我尝试使用一个正则表达式,它给了我一个表达式:/^\/(?:([^\/]+?)\/清单\/?$/i
似乎也无法正常工作。确实可以这样做,但需要导出组件中的路由器
对象:
router.get('/:customer/manifest', function (req, res) { ... })
// ...
module.exports = router
然后导入并像上面那样使用它:
const express = require('express')
cont app = express()
const component = require('./component')
app.use('/:customer/data', component)
您正在代码中添加两条路由,实际路径被复制。实际路径为:
/:customer/manifest/:customer/manifest
您可以采用两种不同的方法
app.js
The setup before this is working correctly, so suppose we only zoom in on the route itself
// Manifest route to retrieve customer context manifest
app.use('/:customer/manifest', manifest);
router.get('/:customer/manifest', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
app.use('/:customer/manifest', manifest);
router.get('/', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
manifest.js
The setup before this is working correctly, so suppose we only zoom in on the route itself
// Manifest route to retrieve customer context manifest
app.use('/:customer/manifest', manifest);
router.get('/:customer/manifest', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
app.use('/:customer/manifest', manifest);
router.get('/', function (req, res) {
console.log('Entering manifest route.');
});
module.exports = router;
第二个路由应该是“/”,因为您在上一个应用程序中指定了它。使用。这对我来说是有意义的,但是我尝试以这种方式实现它,我从未达到实际路由。我正在本地测试它,并尝试执行类似以下操作:http://localhost:9000/rafael/data
但我没有输入路线。我有另一条路线定义为/info
当我输入http://localhost:9000/info
请发布你的app.js
和component.js