Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

express methods having strange behaviors

i have routes with the get method.

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);

all of them works except for app.get("/info/courses/:teacherid", controller10);

but if i move his position to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);

it just works; why is this happening? all the controllers works independtly and dont need information from the others.

>Solution :

There’s no distinction between /info/courses/:teacherid and /info/courses/:courseid. The router will pick the first that matches, and they both match the same urls.

In your second snippet, controller6 will never be called.

You’ll either need to change that route to something like /info/courses/taught/:teacherid (like you did with /enrolled), or you’ll need to have a single route /info/courses/:someId with a controller that can distinguish whether someId is a teacher id or a course id. (Different prefixes, maybe?)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading