Multiple express routers using root path

I am facing the problem of using an express router.

Here's some sample code:

// file.js (1st to be imported)
module.exports = function (app) {
    app.use('/', fileRouter);
};

fileRouter.get('/file', /* do stuff */ );

// user.js
module.exports = function (app) {
    app.use('/', userRouter);
};

userRouter.get('/user', /* do stuff */ );
userRouter.get('/userList', /* do stuff */ );

      

We cannot prefix our routers, and we do not want to change our path architecture.

It seems like two regex routers are being added but duplicated.

It might look like this:

/
=> /file

/
=> /user
=> /userList

      

Instead

/
=> /file
=> /user
=> /userList

      

So instead of looking for two routers, it stops at the first one.

Do you know how we can solve this problem?

+3


source to share


1 answer


If the route matches, and you still want subsequent routes to run if they match, use the third functional parameter of the route callback:



userRouter.get( '/', function( req, res, next ) {  
  /* Do something... */
  next();
});

      

0


source







All Articles