Express.js routes management for large web application

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite
1












I'm currently working on a large web application with numerous routes. Currently, our routes are in one large list in a single JSON file. We recently ran into a problem due to the precedence of params being set from top to bottom in the file. This question details the issue we encountered further: Node.js Express route naming and ordering: how is precedence determined?



My plan is to propose an alternate routing solution based on my work from a personal project. I feel like it's a valid suggestion but missed out on a chance to really make my case in a meeting the other day. Here is a routes.js file from my personal project that demonstrates the pattern I would like to use, let me know what you think or of possible better alternatives/edits. Thanks!



By creating separate routers for each resource, different controllers can access the same URLs without the params being overridden.



const express = require('express');

const usersRouter = express.Router();
const songsRouter = express.Router();
const playlistsRouter = express.Router();

const usersController = require('../controllers/users');
const songsController = require('../controllers/songs');
const playlistController = require('../controllers/playlists');

usersRouter.route('/')
.post(usersController.create)

songsRouter.route('/')
.get(songsController.index)
.post(songsController.create)

playlistsRouter.route('/')
.get(playlistController.index)
.post(playlistController.create)

songsRouter.route('/:id')
.get(songsController.show)
.put(songsController.update)
.delete(songsController.destroy)

playlistsRouter.route('/:id')
.get(playlistController.show)
.put(playlistController.update)
.delete(playlistController.destroy)

module.exports =
songs: songsRouter,
playlists: playlistsRouter,
users: usersRouter,
;


A couple questions that come to my mind are:



  1. How optimal it would be to have all those Express router instances?

  2. Is this approach scalable?






share|improve this question



























    up vote
    3
    down vote

    favorite
    1












    I'm currently working on a large web application with numerous routes. Currently, our routes are in one large list in a single JSON file. We recently ran into a problem due to the precedence of params being set from top to bottom in the file. This question details the issue we encountered further: Node.js Express route naming and ordering: how is precedence determined?



    My plan is to propose an alternate routing solution based on my work from a personal project. I feel like it's a valid suggestion but missed out on a chance to really make my case in a meeting the other day. Here is a routes.js file from my personal project that demonstrates the pattern I would like to use, let me know what you think or of possible better alternatives/edits. Thanks!



    By creating separate routers for each resource, different controllers can access the same URLs without the params being overridden.



    const express = require('express');

    const usersRouter = express.Router();
    const songsRouter = express.Router();
    const playlistsRouter = express.Router();

    const usersController = require('../controllers/users');
    const songsController = require('../controllers/songs');
    const playlistController = require('../controllers/playlists');

    usersRouter.route('/')
    .post(usersController.create)

    songsRouter.route('/')
    .get(songsController.index)
    .post(songsController.create)

    playlistsRouter.route('/')
    .get(playlistController.index)
    .post(playlistController.create)

    songsRouter.route('/:id')
    .get(songsController.show)
    .put(songsController.update)
    .delete(songsController.destroy)

    playlistsRouter.route('/:id')
    .get(playlistController.show)
    .put(playlistController.update)
    .delete(playlistController.destroy)

    module.exports =
    songs: songsRouter,
    playlists: playlistsRouter,
    users: usersRouter,
    ;


    A couple questions that come to my mind are:



    1. How optimal it would be to have all those Express router instances?

    2. Is this approach scalable?






    share|improve this question























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I'm currently working on a large web application with numerous routes. Currently, our routes are in one large list in a single JSON file. We recently ran into a problem due to the precedence of params being set from top to bottom in the file. This question details the issue we encountered further: Node.js Express route naming and ordering: how is precedence determined?



      My plan is to propose an alternate routing solution based on my work from a personal project. I feel like it's a valid suggestion but missed out on a chance to really make my case in a meeting the other day. Here is a routes.js file from my personal project that demonstrates the pattern I would like to use, let me know what you think or of possible better alternatives/edits. Thanks!



      By creating separate routers for each resource, different controllers can access the same URLs without the params being overridden.



      const express = require('express');

      const usersRouter = express.Router();
      const songsRouter = express.Router();
      const playlistsRouter = express.Router();

      const usersController = require('../controllers/users');
      const songsController = require('../controllers/songs');
      const playlistController = require('../controllers/playlists');

      usersRouter.route('/')
      .post(usersController.create)

      songsRouter.route('/')
      .get(songsController.index)
      .post(songsController.create)

      playlistsRouter.route('/')
      .get(playlistController.index)
      .post(playlistController.create)

      songsRouter.route('/:id')
      .get(songsController.show)
      .put(songsController.update)
      .delete(songsController.destroy)

      playlistsRouter.route('/:id')
      .get(playlistController.show)
      .put(playlistController.update)
      .delete(playlistController.destroy)

      module.exports =
      songs: songsRouter,
      playlists: playlistsRouter,
      users: usersRouter,
      ;


      A couple questions that come to my mind are:



      1. How optimal it would be to have all those Express router instances?

      2. Is this approach scalable?






      share|improve this question













      I'm currently working on a large web application with numerous routes. Currently, our routes are in one large list in a single JSON file. We recently ran into a problem due to the precedence of params being set from top to bottom in the file. This question details the issue we encountered further: Node.js Express route naming and ordering: how is precedence determined?



      My plan is to propose an alternate routing solution based on my work from a personal project. I feel like it's a valid suggestion but missed out on a chance to really make my case in a meeting the other day. Here is a routes.js file from my personal project that demonstrates the pattern I would like to use, let me know what you think or of possible better alternatives/edits. Thanks!



      By creating separate routers for each resource, different controllers can access the same URLs without the params being overridden.



      const express = require('express');

      const usersRouter = express.Router();
      const songsRouter = express.Router();
      const playlistsRouter = express.Router();

      const usersController = require('../controllers/users');
      const songsController = require('../controllers/songs');
      const playlistController = require('../controllers/playlists');

      usersRouter.route('/')
      .post(usersController.create)

      songsRouter.route('/')
      .get(songsController.index)
      .post(songsController.create)

      playlistsRouter.route('/')
      .get(playlistController.index)
      .post(playlistController.create)

      songsRouter.route('/:id')
      .get(songsController.show)
      .put(songsController.update)
      .delete(songsController.destroy)

      playlistsRouter.route('/:id')
      .get(playlistController.show)
      .put(playlistController.update)
      .delete(playlistController.destroy)

      module.exports =
      songs: songsRouter,
      playlists: playlistsRouter,
      users: usersRouter,
      ;


      A couple questions that come to my mind are:



      1. How optimal it would be to have all those Express router instances?

      2. Is this approach scalable?








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 3 at 8:43
























      asked May 2 at 3:11









      Aaron Goldsmith

      12510




      12510




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          absolutely, you should compose your express application in terms of a hierarchy of routers



          1. instancing all those routers should be negligible in terms of your application's performance


          2. this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193413%2fexpress-js-routes-management-for-large-web-application%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            absolutely, you should compose your express application in terms of a hierarchy of routers



            1. instancing all those routers should be negligible in terms of your application's performance


            2. this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns






            share|improve this answer



























              up vote
              2
              down vote



              accepted










              absolutely, you should compose your express application in terms of a hierarchy of routers



              1. instancing all those routers should be negligible in terms of your application's performance


              2. this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns






              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                absolutely, you should compose your express application in terms of a hierarchy of routers



                1. instancing all those routers should be negligible in terms of your application's performance


                2. this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns






                share|improve this answer















                absolutely, you should compose your express application in terms of a hierarchy of routers



                1. instancing all those routers should be negligible in terms of your application's performance


                2. this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 4 at 21:23


























                answered May 4 at 20:27









                ChaseMoskal

                1364




                1364






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193413%2fexpress-js-routes-management-for-large-web-application%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation