Express.js routes management for large web application
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
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:
- How optimal it would be to have all those Express router instances?
- Is this approach scalable?
javascript node.js ecmascript-6 express.js url-routing
add a comment |Â
up vote
3
down vote
favorite
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:
- How optimal it would be to have all those Express router instances?
- Is this approach scalable?
javascript node.js ecmascript-6 express.js url-routing
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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:
- How optimal it would be to have all those Express router instances?
- Is this approach scalable?
javascript node.js ecmascript-6 express.js url-routing
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:
- How optimal it would be to have all those Express router instances?
- Is this approach scalable?
javascript node.js ecmascript-6 express.js url-routing
edited May 3 at 8:43
asked May 2 at 3:11
Aaron Goldsmith
12510
12510
add a comment |Â
add a comment |Â
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
instancing all those routers should be negligible in terms of your application's performance
this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns
add a comment |Â
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
instancing all those routers should be negligible in terms of your application's performance
this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns
add a comment |Â
up vote
2
down vote
accepted
absolutely, you should compose your express application in terms of a hierarchy of routers
instancing all those routers should be negligible in terms of your application's performance
this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns
add a comment |Â
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
instancing all those routers should be negligible in terms of your application's performance
this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns
absolutely, you should compose your express application in terms of a hierarchy of routers
instancing all those routers should be negligible in terms of your application's performance
this is the only approach i can imagine feasibly maintaining at scale, it buys you some separation of concerns
edited May 4 at 21:23
answered May 4 at 20:27
ChaseMoskal
1364
1364
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password