1

I know this is a slightly subjective question, but I would appreciate insight from experienced web app engineers.

I am currently working on a medium size webapp in node.js with a REST API/Single Page App type of architecture. On the server side I am always struggling to know when it is the best time to split a single file and use a require() to call a block of code.

I understand that file complexity is unlikely to affect the final production server performance, I want to know the goldilocks range to balance readability with directory structure complexity.

There is a similar question aimed specifically at classical languages like vb.net here and there is an excellent question about web application architecture here.

Community
  • 1
  • 1
lindsaymacvean
  • 3,741
  • 2
  • 14
  • 19

2 Answers2

1

Rather than counting classes or lines, I'd consider how much of the content of a specific file can be abstracted out into its own module based on what it is supposed to accomplish or provide.

If your app.js contains a thousand routes, it might make sense to place those routes into a separate "require"-able file.

If several of your routes are concerned with accessing/manipulating configuration for your application, they might be candidates for their own file as well.

As far as performance goes, you'll only take a hit at program load as, in the typical case, node will load all your requirements as each file in your application is loaded.

The rule I've always followed is: package enough code into a single file so that, as a whole, it provides one function or service and nothing other than that service, especially if that service can be reused in other applications.

In essence, the proper number of lines per file is that which allows the file to accomplish its goal and no more.

How you decide what your code is supposed to accomplish and how you might break a potentially complex application into abstractable parts is entirely up to you.

Rob Raisch
  • 15,416
  • 3
  • 43
  • 55
0

I don't think there are any performance constraints about that. To my point of view, a good separation of files (which implies that the code is better organized and you do less useless things) is more efficient than limit the 'require' usage. Personally I split my files when the content is not related with the rest of the file. In order to do clean code, I personally separate my code in three files in my API's endpoints. Let's take an example with a user:

  • index.js (which contains my route definitions). I require my user controller function.

var controller = require('./user.controller'); var router = express.Router(); router.get('/:id', userController.get);

  • user.controller.js (which contains the logic of the user on the server-side). The user controller will call the user model definition var User = require('./user.model');
  • user.model.js (which contains the user model definition - working with MongoDB in this case)

There is a lot of guides of good practices on the web. My best advice is to look at what is generated with the boilerplate angular-fullstack, the server-side part which applies the Node.js good practices. (https://github.com/DaftMonk/fullstack-demo/tree/master/server). Once you will be familiar with the general structure, you will be able to create your own which will correspond to your needs, there is no universal response.