如何组织大型节点应用
How to organize large-scale node apps
场景:
我想保持一个干净的结构,比如
/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
目前我正在使用 require
和 module.exports
。它有效,但我很好奇社区在我阅读的内容之外做了什么。
问题:
组织中型到大型 js 应用程序(30+ classes)同时保持良好的性能和整体组织的好方法是什么?
当前实施
根据我上面的例子我会做
Events.js
class Events {
...
}
module.exports = Events;
然后对于每个使用 Events
的 class
const Events = require("/org/myapp/events/Events.js");
你所做的工作正常,但我个人更喜欢让每个目录(例如 /org/myapp/events
)都有一个 index.js 文件,该文件在文件夹中导出 类,例如所以:
// /org/myapp/events/index.js
module.exports = {
EventDispatcher: require('./event-dispatcher.js')
}
然后消费代码可以一次请求整个目录,然后根据需要访问部分:
const events = require('./events');
const dispatcher = new events.EventDispatcher();
场景:
我想保持一个干净的结构,比如
/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
目前我正在使用 require
和 module.exports
。它有效,但我很好奇社区在我阅读的内容之外做了什么。
问题:
组织中型到大型 js 应用程序(30+ classes)同时保持良好的性能和整体组织的好方法是什么?
当前实施
根据我上面的例子我会做
Events.js
class Events {
...
}
module.exports = Events;
然后对于每个使用 Events
const Events = require("/org/myapp/events/Events.js");
你所做的工作正常,但我个人更喜欢让每个目录(例如 /org/myapp/events
)都有一个 index.js 文件,该文件在文件夹中导出 类,例如所以:
// /org/myapp/events/index.js
module.exports = {
EventDispatcher: require('./event-dispatcher.js')
}
然后消费代码可以一次请求整个目录,然后根据需要访问部分:
const events = require('./events');
const dispatcher = new events.EventDispatcher();