Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda