我有 get 方法的路線。
app.get("/info/teachers", controller1);
app.get("/info/teachers/:teacherid", controller2);
app.get("/info/students", controller3);
app.get("/info/students/:studentid", controller4);
app.get("/info/courses", controller5);
app.get("/info/courses/:courseid", controller6);
app.get("/info/courses/enrolled/:studentid", controller7);
app.get("/info/assists/teachers", controller8);
app.get("/info/assists/teachers/:teacherid", controller9);
app.get("/info/courses/:teacherid", controller10);
app.get("/info/assists/students", controller11);
app.get("/info/assists/students/:studentid", controller12);
除了 app.get("/info/courses/:teacherid", controller10);
但如果我將他的位置移動到:
app.get("/info/teachers", controller1);
app.get("/info/courses/:teacherid", controller10);
app.get("/info/teachers/:teacherid", controller2);
app.get("/info/students", controller3);
app.get("/info/students/:studentid", controller4);
app.get("/info/courses", controller5);
app.get("/info/courses/:courseid", controller6);
app.get("/info/courses/enrolled/:studentid", controller7);
app.get("/info/assists/teachers", controller8);
app.get("/info/assists/teachers/:teacherid", controller9);
app.get("/info/assists/students", controller11);
app.get("/info/assists/students/:studentid", controller12);
它只是作業; 為什么會這樣?所有控制器獨立作業,不需要其他控制器的資訊。
uj5u.com熱心網友回復:
/info/courses/:teacherid和之間沒有區別/info/courses/:courseid。路由器將選擇第一個匹配的,并且它們都匹配相同的 url。
在您的第二個片段中,controller6將永遠不會被呼叫。
您要么需要將該路由更改為類似的東西/info/courses/taught/:teacherid(就像您對 所做的那樣/enrolled),或者您需要有一個/info/courses/:someId帶有控制器的單個路由,該控制器可以區分someId是教師 ID 還是課程 ID。(不同的前綴,也許?)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/414726.html
標籤:
