1

(Is it recommended to do so)

I feel the code for the routes could be easier to read and debug are further modularised. Is it a good practice to do so?

We have the usual code

In app.js

var users = require('./routes/users');
app.use('/users', users);

In the users.js file I have the code for these routes:

/user
/user/bar
/user/42
/user/42/baz
/user/42/baz/123

I gave code for get, post, put and use. And I feel the file is too messy.

Is it recommended to split the ./routes/users.js further and is there a design pattern for it?

Edit as per requested though this was a generic question

var express = require("express");
var marked = require("marked");
var OfflineMediaModel = require("../models/offlineMediaModel");
const offlineMediaRouter = express.Router();
var fs = require("fs");
var md = require("node-markdown").Markdown;
var path = require("path");
var config = require("../config");

// Middleware
offlineMediaRouter.use("/", function(req, res, next) {});
offlineMediaRouter.use("/podcasts/test", (req, res, next) => {});
offlineMediaRouter
  .post("/podcasts/test", function(req, res) {}) /// this is the only post
  .get("/", function(req, res) {})
  .get("/:podcast", function(req, res) {})
  .use("/:podcast/:episode", function(req, res, next) {})
  .get("/:podcast/:episode", function(req, res) {});
module.exports = offlineMediaRouter;

I'm wondering in much bigger projects that that, would further modularising that code be a good idea or is it not advised to do so? Or should the code within the routes methods

relidon
  • 2,142
  • 4
  • 21
  • 37
  • Are you using models in your program? – Y4glory Apr 08 '20 at 18:56
  • @Y4glory Not sure, what are they? (I'm relatively new at this) – relidon Apr 08 '20 at 18:58
  • Please post the contents of the file that you think is too messy. We cannot advise on things we cannot see, and no, "split it into more files" is not a good generic advice. – Bergi Apr 08 '20 at 19:02
  • I agree with @Bergi , I just wanted to know if you had some sort of an architecture to begin with. – Y4glory Apr 08 '20 at 19:03
  • Also there are a lot of answers to your question as it may involve changes in how your app is structured, how you write different parts of your code, or maybe even just plain old abstraction. So it would help seeing the file and what you are trying to achieve. I would also recommend looking into https://stackoverflow.com/questions/41875617/building-enterprise-app-with-node-express this question, there may be a lot of things going on here but if you look at the question and search for what the person was trying to do you may stumble over a lot of these answers I'm talking about – Y4glory Apr 08 '20 at 19:11
  • If you have working code and you want ideas for improving the code, then post your users.js file in http://codereview.stackexchange.com. No matter where you want help, we would able to offer a lot more intelligent advice if we could see the actual file you're asking about. As with nearly all design decisions, there's nothing absolute - it's about seeing what you're' trying to accomplish and coming up with the cleanest ways to do that. Seeing your code is the best way to start. – jfriend00 Apr 08 '20 at 19:17
  • You split a routes file when: a) it feels like it's getting bigger than you like and b) when you have a nice logical feeling way of splitting it that doesn't cause more complication than not splitting it or c) there's some amount of code that you want to be able to share that can/should be split out into its own module. The logic for when to split a routes file is pretty much the same as when you split any other code file. – jfriend00 Apr 08 '20 at 19:18
  • @jfriend00 that's good to know. I wasn't actually looking for concrete help (and I realise it violates the site rules) I just wanted to know if `users` route folder handles tons of http methods, is there a design pattern to split it – relidon Apr 08 '20 at 19:20
  • What you just added to your question doesn't look overwhelming to me at all and seems perfectly appropriate for one file. I like to be able to see what one particular router is doing in one file as long as the implementation doesn't get unusually large. Otherwise, it's harder to get a feeling for what the router is doing if you have to look across multiple files to see that. – jfriend00 Apr 08 '20 at 19:21
  • A design for splitting a router would be to import sub-files which export a function and after importing, you call that exported function, passing them the router you created and that function in the subfiles would add its methods to the router. As in `require('./router-subfile1")(router)`. – jfriend00 Apr 08 '20 at 19:22

1 Answers1

0

When to split a router file into more files is really just like deciding when to split any other coding file.

  1. When it feels like the file you have is getting too big for easy maintenance and navigation.
  2. When there are boundaries in who is working on what code or responsible for what code that facilitate easier team development.
  3. When splitting it will make things simpler than the extra work and complication involved in doing the splitting and using multiple files.
  4. When splitting doesn't create unnecessary complications with branching, merging other code efforts underway in a team development environment.
  5. When there's some amount of code in the module that you'd like to share with other modules or independently test.
  6. When shared code between pieces you would split doesn't overly complicate the split.

One fairly simple design for splitting a router file is to have one main router file that creates the router and exports it and that's what your main app knows about and loads.

// main-router.js
const router = require('express').Router();

// some route definitions here

module.exports = router;

Then, you can create sub-route files:

// sub-router1.js
module.exports = function(router) {
    router.get(path1, ...)

    router.post(path2, ...)
}

Then, you integrate sub-router1.js into your main router file like this:

// main-router.js
const router = require('express').Router();

// possibly some route definitions here

// load and initialize sub-router1.js
require('./sub-router1.js)(router);

// possibly some route definitions here

module.exports = router;

Keep in mind that when you break routes into sub-files, you must do so in a way that allows you to sequence the route definitions appropriately since you will only be able to sequence within the individual files and sequence how the files are loaded. So, overlapping routes that have a particular sequence requirement should probably be in the same file where their order can be controlled precisely.

jfriend00
  • 683,504
  • 96
  • 985
  • 979