我写了一些随机的东西练的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", ... )
注册中间件将永远不会达到。
在你的第一个例子中,第一条路线“/ animes /:id为”如果你叫“animes /加”相匹配。 “添加”将是在这种情况下,ID参数。在谢胜利路线将被忽略。