Home > Blockchain >  Best Practice for Route Architecture
Best Practice for Route Architecture

Time:05-21

I'm trying to set up routes for my backend. I have two ways that I've tried setting these routes up, and I'm wondering which way fits best practices (or neither?). The differences are minimal, but I'd love to know if there is an objective "best" here.

Here are my attempts:

const express = require("express");
const router = express.Router();
const flashcardController = require('../controllers/flashcardController');

router.get('/', flashcardController.readFlashcard);
router.post('/', flashcardController.createFlashcard);
router.patch('/', flashcardController.updateFlashcard);
router.delete('/', flashcardController.deleteFlashcard);

module.exports = router

VS

const express = require("express");
const router = express.Router();
const flashcardController = require('../controllers/flashcardController');


module.exports = (app) => {
    router.get('/api/flashcard', flashcardController.readFlashcard);
    router.post('/api/flashcard', flashcardController.createFlashcard);
    router.patch('/api/flashcard', flashcardController.updateFlashcard);
    router.delete('/api/flashcard', flashcardController.deleteFlashcard);

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

Of course, my app.js (entry-point for my backend) file will need to be coded slightly differently for each of these options.

CodePudding user response:

If you believe that the job of a router is to just handle some requests that it receives and it is the job of the calling code to place the router at whatever path the calling code wants it to operate on, then only your first option will do that. This would allow a caller to use these routes in whatever path it wants.

If you want the module that implements the routes to be entirely self-sufficient and install the routes on the path it wants them to be on, then only the second option does that.

I would say that the "usual" and more "flexible" scheme is the first one where the caller places the routes on the path where it wants them. But, you are free to choose whichever style you want.

The second option is not implemented particularly efficiently so it could be improved. No router is needed at all as the routes can be just installed directly on the app object directly. And, repeating /api/flashcard multiple times can be avoided.

For example, the second option could be this:

const controller = require('../controllers/flashcardController');
const routePath = '/api/flashcard';

module.exports = (app) => {
    app.get(routePath, controller.readFlashcard);
    app.post(routePath, controller.createFlashcard);
    app.patch(routePath, controller.updateFlashcard);
    app.delete(routePath, controller.deleteFlashcard);
};

Or, even just this:

const controller = require('../controllers/flashcardController');

module.exports = (app) => {
    app.route('/api/flashcard')
        .get(controller.readFlashcard)
        .post(controller.createFlashcard)
        .patch(controller.updateFlashcard)
        .delete(controller.deleteFlashcard);
};
  • Related