表示参数化的路由冲突
我在Express 4.13应用程序中有两条路线:
router.get('/:id', function (req, res) { }); router.get('/new', function(req,res){ });
但是当我试图访问/new
– 我得到404,因为没有“新”的对象。 那么我怎样才能改变设置,我可以访问/新路线而不会混淆/:id路由。
谢谢。
你需要添加一个函数来检查参数,并在/new
之前放置/new
路由器/:id
:
var express = require('express'), app = express(), r = express.Router(); r.param('id', function( req, res, next, id ) { req.id_from_param = id; next(); }); r.get("/new", function( req, res ) { res.send('some new'); }); // route to trigger the capture r.get('/:id', function (req, res) { res.send( "ID: " + req.id_from_param ); }) app.use(r); app.listen(3000, function () { })
像这样做 。 dynamicAPI应该在底部
router.get('/new', function(req,res){ }); router.get('/:id', function (req, res) { });