Reputation: 3
I'm using nodejs (with sailsjs), and I'm having an issue with routing, here is the case:
'/user/?variable'
. It links to
a controller action that shows a profile, depending on the variable
given; if no user is found, it returns a 404 error.'/user/find'
. It is linked to a
controller action meant for an AJAX request that returns the user's
id given the variables passed to it.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
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