Alfreth
Alfreth

Reputation: 3

What to do when one route "blocks" another?

I'm using nodejs (with sailsjs), and I'm having an issue with routing, here is the case:

However, when I call '/user/find/' with the AJAX request, then I get the 404 error; I guess this is because it still links to the "show profile" action, and the action can't find an user named "find".

Is this conflict common among frameworks? Are there ways to solve this? I've tried switching the order in which I declare the routes, but the response is the same. I guess it would probably cause conflict too if an user signs up with username 'find', in that case, how would I handle it? Or should I just use a completely different route?

I like /user/find because the name is very straightforward, though.

Thank you.

Upvotes: 0

Views: 64

Answers (1)

Jeff Sloyer
Jeff Sloyer

Reputation: 4964

When you declare your routes in your app.js or whatever file you file order matters.

app.get("/user/find", function (request, response) {
    //do something
});

app.get("/user/:variable", function (request, response) {
    //do something
});

In the above example /user/find takes precedence over /user/:variable because it is declared first. If you need to do this I would suggest playing with the order of the declarations. If you switch it to the following it should work.

app.get("/user/:variable", function (request, response) {
    //do something
});

app.get("/user/find", function (request, response) {
    //do something
});

Upvotes: 1

Related Questions