Sunday 28 May 2017

Appending a Prefix to Routes in One File in Express JS

I am currently refactoring my code and I am hoping there is a more efficient way to make my code look pretty. Here is what I currently have:

router.route('/api/merchant/')
        .get(controllerFunction)
        .post(controllerFunction);

router.route('/api/user/')
        .get(controllerFunction)
        .post(controllerFunction);

router.route('/admin/user/')
        .get(controllerFunction)
        .post(controllerFunction);

router.route('/admin/place/')
        .get(controllerFunction)
        .post(controllerFunction);

You can see that I have "api" and "admin" as prefixes to certain routes.

I can separate the API route into another file and have the "app.use" middleware append them appropriately, but I was wondering how can I get around this and have these routes in a single file with the main problem being at the end of the file:

module.exports = router

which only allows me to have one prefix to all my routes.



via Anraiki

No comments:

Post a Comment