Routing in Express.js

59 views Asked by At

I am currently express router in Node.js and having a problem like below. Let's say I have two url; one is to get a user info and one is to register users to an application.

http://example.com/users/:idUser (this will give a information of a user)

http://example.com/users/registration (this will allow a user registration)

The problem I have facing is when I call registration, the router is working with idUser; so I had to edit like user/registration instead of users. If I want to use as users/registration, which kinds of work do I have to do. I am still a newbie in Node.js.

Thanks.

2

There are 2 answers

2
Bidhan On BEST ANSWER

You need to order the routes appropriately so that the registration route comes before.

app.get('/users/registration', function(req, res, next) {
    ....
});

app.get('/users/:userId', function(req, res, next) {
   ....
});
0
neiker On

Just invert the order, like this:

app.get('/users/registration', function(req, res, next) {
    ...
});
app.get('/users/:userId', function(req, res, next) {
    ...
});