Reputation: 6144
this is my situation with routes. I am already handling routes with static sites (rendering site) like
domain.com/sign-in
domain.com/cookie-policy
Now I am thinking of creating route with id like
domain.com/:id
domain.com/john-and-anna
domain.com/something-different
Id must be unique, the users will choose it, so I can make a collection in mongodb and set id to be unique.
Problem is that in my opinion Express Router will handle sign-in
and cookie-policy
also as ids. So, I need to handle them differently - every time check if user didn't set their id to sign-in
or cookie-policy
(or another page) and that it is still unique. Kind of a mess.
I could solve all these issues by setting new route
domain.com/string/:id
but this is not what I want in first goal.
Is there any way to handle this in a better way? Thank you in advance.
Upvotes: 1
Views: 145
Reputation: 708146
domain.com/:id
is generally a bad idea unless you have a simple rule that allows you to distinguish an id from a non-id (such as ids are only numbers and non-ids are never numbers). But, if you're allowing users to choose their own id and not enforcing some strict rule that lets you identify purely from a string what is and isn't an id, then you really should not do domain.com/:id
because that will easily be confused with all your other top level resources.
It would be much better to do domain.com/id/:id
where it's 100% obvious when an id is being requested and when it's something else and then an id can unambiguously be anything any user wants it to be (that is allowed in a URL). Think about both now and the future when you want to add more pages to your site so that you never have an ambiguity between which request is requesting an id and which is requesting some other resource.
If you really wanted to allow your users to have domain.com/theirchoicehere
, then you'd have to put all your other static resources in their own sub-path such as domain.com/sys/login
and domain.com/sys/logout
and disallow users from choosing things like sys
so there's no conflict between ids and your site urls. The point here is that the pathnames the users choose and the names you use to manage your web site need to be in a separate namespace (not in the same path position). So, if you want one of them at the top level, then decide which one is at the top level and force the other one to the second level.
Routes are matched in order so you can put the more specific routes first and the less specific routes (the ones with :id
in them) later, but you still have to design your path structure carefully so there are no ambiguities.
Upvotes: 2
Reputation: 318
You can try setting your app.get("/sign-in");
and your app.get("/cookie-policy");
before your app.get("/:id");
.
Express will handle routes in the order they are defined. This is not going to work 100%, since I am not able to test it right now, but you can try it out!
Edit: Your best bet is to just create a single handler /:id and check if :id is sign-in or cookie-policy and handle them diffrently from the other ids.
Upvotes: 2
Reputation: 8369
Express match incoming routes in the order, you should put domain.com/:id
route handler at the end if you want other routes to work:
domain.com/john-and-anna
domain.com/something-different
domain.com/:id
Upvotes: 0