Anraiki
Anraiki

Reputation: 796

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.

Upvotes: 0

Views: 3148

Answers (3)

abolfazl maherani
abolfazl maherani

Reputation: 168

Short answer for your question follow below

  1. You can put your middleware in separate file.

[name]Controller.js

module.exports = (req, res)=>{
  res.send('I am a middleware');
}
  1. Create a file bootable routers and export an array from routers.

boot.js

const express = require('express'),
  router = express.Router(),
  [name]Controller = require('./controller/[name]Controller'),
  module.exports = [
    router.get('/your path', [name]Controller),
    ...
  ]
  1. Use router in your app file.

app.js

 const express = require('express'),
     boot = require('./boot'),
     app = express();
     app.use('/prefix', boot);
 

Now you can use routes with prefix for example: localhost:3000/prefix/yourpath

Upvotes: 1

Paul
Paul

Reputation: 36329

You can mount the whole router on a path. If you do something like:

router.get('/:id', someFn);

Then elsewhere you could do:

app.use('/api', router);

and the route '/api/:id/` would resolve to the someFn handler

UPDATE

I'm not sure I fully understand your concern, and frankly having all your routes in one file makes for a file longer than I'm comfortable with, but if you really only want to mount one router, you can do something similar to what I described, but inside your routes file.

In that case, you'd still create the multiple routers but then rather than exporting them and mounting on the app, you'd do this:

const adminRouter = express.Router();
// configure the admin routes as I described 

const apiRouter = express.Router();
// configure API routes

const main = express.Router();

main.use('/admin', adminRouter);
main.use('/api', apiRouter);

module.exports = main;

Upvotes: 0

hoangdv
hoangdv

Reputation: 16137

You can folow below block code, but I suggest you write controler function to another file each for once.

        var router = express.Router();
        var useApi = router.route('/user')
                .get(controllerFunction)
                .post(controllerFunction);

        var useAdmin = router.route('/user')
                .get(controllerFunction)
                .post(controllerFunction);

        app.use('/api', userApi);
        app.use('/admin', userAdmin);

Upvotes: 0

Related Questions