Jacksonkr
Jacksonkr

Reputation: 32207

How to organize large-scale node apps

Scenario:

I want to keep a clean structure, something like

/org/myapp/events/Events.js
/org/myapp/events/EventDispatcher.js
/org/myapp/game/Game.js
/org/myapp/characters/Worker.js
/org/myapp/characters/Warrior.js
/org/myapp/characters/Elite.js
etc etc

Currently I'm using require, and module.exports. It works but I'm curious as to what the community is doing outside of what I've been reading.

Question:

What is a good way to organize a medium to large scale js application (30+ classes) while keeping a good handle on performance as well as overall organization?

Current Implementation

Based on my example above I would be doing

Events.js

class Events {
    ...
}

module.exports = Events;

Then for every class that uses Events

const Events = require("/org/myapp/events/Events.js");

Upvotes: 0

Views: 144

Answers (1)

Paul
Paul

Reputation: 36319

What you're doing works fine, but personally I prefer to have each directory (e.g. /org/myapp/events) have an index.js file which exports the classes within the folder, like so:

// /org/myapp/events/index.js
module.exports = {
  EventDispatcher: require('./event-dispatcher.js')
}

Then consuming code can require the whole directory once and then access the parts as needed:

const events = require('./events');

const dispatcher = new events.EventDispatcher();

Upvotes: 1

Related Questions