Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Best Practice for Route Architecture

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
};

And, the first one could be simplified to this:

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

router.route('/')
  .get(controller.readFlashcard)
  .post(controller.createFlashcard)
  .patch(controller.updateFlashcard)
  .delete(controller.deleteFlashcard);

module.exports = router
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!