In my express application, I have set up various routes to handle user-related tasks.
router.route('/login').post(userCtrl.login);
router.route('/users').get(userCtrl.getAll);
router.route('/users/count').get(userCtrl.count);
router.route('/user').post(userCtrl.insert);
router.route('/user/:id').get(userCtrl.get);
router.route('/user/:id').put(userCtrl.update);
router.route('/user/:id').delete(userCtrl.delete);
Now, I want to add a middleware that will be applied to all routes starting with '/user' except for the '/login' route.
For example, I have a middleware called checkAuth
that should be used for any user-specific routes but not for the /login
route.
I tried to implement this using the code below, but it didn't work as expected.
router.use("/user.*", checkAuth);
Is there a way in express to configure middleware based on a specific pattern of routes?