为什么带参数的动词方法阻止另一个同样的路线吗?
我写了一些随机的东西练的Node.js和express.js。所以之后我写了一个get方法与参数,它阻止运行另一个get方法。我想知道为什么。
我相信,这是因为第一次的方法。我删除它和之后的第二移动它,和它的工作就好了。但是,当第二个之前的,它阻止它。
// the following code is the one that blocks
app.get("/animes/:id", (req, res)=>{
res.send(animes[req.params.id]);
});
app.get("/animes/add", (req, res)=>{
console.log(req.query);
res.send("yes")
});
// the following code works fine
app.get("/animes/add", (req, res)=>{
console.log(req.query);
res.send("yes")
});
app.get("/animes/:id", (req, res)=>{
res.send(animes[req.params.id]);
});
我有一个途径,如“/”和“animes”另外两个get方法。我相信,他们不是它为什么块的原因。
回答如下:中间件在他们注册的顺序进行评估。
因此对于:
app.get("/animes/:id", ... )
app.get("/animes/add", ... )
Express将第一次测试,如果请求的URL匹配/animes/:id
和/animes/:id
将匹配/animes/add
,与app.get("/animes/add", ... )
注册中间件将永远不会达到。
为什么带参数的动词方法阻止另一个同样的路线吗?
我写了一些随机的东西练的Node.js和express.js。所以之后我写了一个get方法与参数,它阻止运行另一个get方法。我想知道为什么。
我相信,这是因为第一次的方法。我删除它和之后的第二移动它,和它的工作就好了。但是,当第二个之前的,它阻止它。
// the following code is the one that blocks
app.get("/animes/:id", (req, res)=>{
res.send(animes[req.params.id]);
});
app.get("/animes/add", (req, res)=>{
console.log(req.query);
res.send("yes")
});
// the following code works fine
app.get("/animes/add", (req, res)=>{
console.log(req.query);
res.send("yes")
});
app.get("/animes/:id", (req, res)=>{
res.send(animes[req.params.id]);
});
我有一个途径,如“/”和“animes”另外两个get方法。我相信,他们不是它为什么块的原因。
回答如下:中间件在他们注册的顺序进行评估。
因此对于:
app.get("/animes/:id", ... )
app.get("/animes/add", ... )
Express将第一次测试,如果请求的URL匹配/animes/:id
和/animes/:id
将匹配/animes/add
,与app.get("/animes/add", ... )
注册中间件将永远不会达到。