如何组织大型 Node.js 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9832019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to organize large Node.js projects
提问by Thomas Hunter II
What are some good ways to organize large Node.js projects?
有哪些组织大型 Node.js 项目的好方法?
For example, an app making use of both express.js and socket.io? This would include both application logical structure as well as filesystem.
例如,一个同时使用 express.js 和 socket.io 的应用程序?这将包括应用程序逻辑结构和文件系统。
Currently, I'm finding myself shoving a ton of code into a single master js file and placing code into a giant global object, and it feels naughty.
目前,我发现自己将大量代码塞进一个单一的主 js 文件并将代码放入一个巨大的全局对象中,这感觉很顽皮。
回答by Jason Sebring
A Beginners Example
初学者示例
I like the originally checked from @david-ellis and you should study it in depth to understand it as it is a good one. However, I would have liked it more simplified for the beginners wanting to see a straight forward example. Here's what I would have liked to have seen someone show me.
我喜欢 @david-ellis 最初检查的内容,你应该深入研究它以理解它,因为它是一个很好的。但是,对于希望看到一个直接示例的初学者,我希望它更简化。这是我希望看到有人向我展示的内容。
Let's give a typical scenario where you are using express and you have a lot of routes listed on your app.js file. Its contents would look something like this:
让我们举一个典型的场景,您使用 express 并且在 app.js 文件中列出了很多路由。它的内容看起来像这样:
app.js
应用程序.js
// ... startup code omitted above
app.get('/', function(req, res) {
res.render('index', { title : 'home' });
});
app.get('/contactus', function(req, res) {
res.render('contactus', { title : 'contact us' });
});
app.get('/anotherpage', function(req, res) {
res.render('anotherpage', { title : 'another page' });
});
// and so on...
You can imagine if you have 50 routes, this file can get quite out of hand. It would be nice to remove some of this clutter out of the app.js file.
你可以想象如果你有 50 条路由,这个文件会变得非常失控。从 app.js 文件中删除一些这种混乱会很好。
What you would do is create a "controllers" folder in your app so your structure would now look like this:
你要做的是在你的应用程序中创建一个“控制器”文件夹,这样你的结构现在看起来像这样:
app.js
/controllers
Create a file within "/controllers" named "index.js" then put the following code.
在“/controllers”中创建一个名为“index.js”的文件,然后输入以下代码。
/controllers/index.js
/控制器/index.js
module.exports.set = function(app) {
// copy your routes listed in your app.js directly into here
}
Cut and paste your route listings from your "app.js" file and place them into the "/controllers/index.js" file.
从“app.js”文件中剪切并粘贴路由列表,并将它们放入“/controllers/index.js”文件中。
On your app.js file, remove your routes and in place of them do the following.
在您的 app.js 文件中,删除您的路由并代替它们执行以下操作。
app.js
应用程序.js
// remove your routes and replace with this code
var controllers = require('./controllers');
controllers.set(app);
Now if you wanted to have your "/controllers/index.js" file also be split up, let's add one more example so you can see how Node.js really acts like a Russian Doll in how its code can be organized.
现在,如果您还想拆分“/controllers/index.js”文件,让我们再添加一个示例,这样您就可以了解 Node.js 在其代码的组织方式方面如何真正像俄罗斯娃娃一样工作。
Within "/controllers" add one more file "accounts.js" and place the following within it.
在“/controllers”中再添加一个文件“accounts.js”并将以下内容放入其中。
/controllers/account.js
/控制器/account.js
module.exports.set = function(app) {
// put more app route listings here
}
Now within your "/controllers/index.js file, put a reference to "account.js"
现在在你的“/controllers/index.js”文件中,引用“account.js”
/controllers/index.js
/控制器/index.js
var account = require('./account.js');
module.exports.set = function(app) {
// your routes here
// let "account.js" set other routes
account.set(app);
}
As you can imagine, you can keep breaking things up into smaller and smaller parts and put more folders within folders and reference with "require" if you like. You can use the same concept for "/lib" or library files. "node_modules" is already doing this.
如您所想,您可以继续将内容分解成越来越小的部分,并在文件夹中放置更多文件夹,如果您愿意,可以使用“require”进行引用。您可以对“/lib”或库文件使用相同的概念。“node_modules”已经在这样做了。
That is just one of many reasons node.js is very enjoyable to program with.
这只是 node.js 编程非常有趣的众多原因之一。
Manageable Express 4 Routing example
Manageable Express 4 路由示例
Here's another post I responded to about express 4 routes that relates to this.
这是我回复的另一篇关于与此相关的 express 4 路线的帖子。
回答by mna
I wrote a blog post about this very subjecta few days ago, and although the article is in French, I set up a GitHub repo(in English) to show a working example of the structure I use.
几天前我写了一篇关于这个主题的博客文章,虽然这篇文章是法语,但我设置了一个GitHub 存储库(英语)来展示我使用的结构的工作示例。
Obviously, there is no definitive answer to this question, but it's interesting to see what others are doing, and I am all ears for other opinions on the subject (which was also discussed here, where you can see a summary of what I suggest).
显然,这个问题没有明确的答案,但是看看其他人在做什么很有趣,而且我对这个主题的其他意见全神贯注(这里也讨论过,你可以在那里看到我建议的摘要) .
回答by Ryan
Similar to the other blog post, I wrote one specifically about organizing Expressapplications. It's the method I've been using for about a year and a half. Basically, organize your applications around your data entities or any other core elements. Place logic for each of those elements in their own directories. I tried to borrow a lot from Python.
与另一篇博文类似,我专门写了一篇关于组织Express应用程序的文章。这是我使用了大约一年半的方法。基本上,围绕您的数据实体或任何其他核心元素组织您的应用程序。将每个元素的逻辑放在它们自己的目录中。我试图从 Python 中借用很多。
回答by Devnetics
His Articles are no longer online, but Eric Satterwhite's node series recommended a structure as listed below.
他的文章不再在线,但 Eric Satterwhite 的节点系列推荐了如下所列的结构。
# Project
.
|-- packages/
| |-- project-core
| | |-- lib/
| | |-- commands/
| | |-- startup/
| | |-- conf/
| | |-- test/
| | |-- package.json
| | |-- README.md
| | |-- events.js
| | |-- .npmignore
| | `-- index.js
|-- package.json
`-- index.js
With the packages/folder turning into your source for modularity.
随着packages/文件夹变成您的模块化来源。
回答by Yevhenii Herasymchuk
If you are a beginner in this area, I suggest you take a look at existing projects that are loved by developers. Some of them are:
如果你是这方面的初学者,我建议你看看现有的开发者喜爱的项目。他们之中有一些是:
Sails.js- 18k stars. You can take a look how they organized APP structure at the link I provided. A great website has an explanation for each folder in the structure.
Express.js generator- 800 stars. There is the great and simple template to start working with Express.js. Here you can notice how they split routes from the app.
Sails.js- 18k 星。您可以在我提供的链接中查看他们如何组织 APP 结构。一个伟大的网站对结构中的每个文件夹都有解释。
Express.js 生成器- 800 颗星。有一个伟大而简单的模板可以开始使用 Express.js。在这里您可以注意到他们如何从应用程序中分割路线。
By the way, so many developers did your case before, and you can just fork it and upgrade.
顺便说一句,之前有很多开发人员做过你的案例,你可以分叉并升级。
Kioska. Look how they separate events into different files at the
events/folder.Ballons.io2.3k stars. Unfortunately, they have the whole socket server in one file but you can learn the whole structure of the app with node.js and socket.io
亭。看看他们如何将事件分离到文件
events/夹中的不同文件中。Ballons.io2.3k 星。不幸的是,他们将整个套接字服务器放在一个文件中,但您可以使用 node.js 和 socket.io 了解应用程序的整个结构
回答by Krzysztof Szostak
If you don't mind, you can always learn typescript and go for https://nestjs.com/
如果您不介意,您可以随时学习打字稿并访问https://nestjs.com/
If you want to stick with JS I strongly recommend to use onion architecture. The best practice is to keep separated business logic, controllers, even libraries (they should be wrapped inside of some classes / helpers) - just in case, if you will have to migrate to another library (different CSV parser etc.).
如果您想坚持使用 JS,我强烈建议您使用洋葱架构。最佳实践是保持独立的业务逻辑、控制器,甚至库(它们应该被包装在一些类/帮助程序中)——以防万一,如果你必须迁移到另一个库(不同的 CSV 解析器等)。
With onion architecture, you don't care from where requests comes from, there won't be a lot of changes to add even some message broker.
使用洋葱架构,您不关心请求来自哪里,甚至不会有很多更改来添加一些消息代理。
Also try this one https://en.wikipedia.org/wiki/Domain-driven_design
也试试这个https://en.wikipedia.org/wiki/Domain-driven_design
ESLint may help with proper project organization.
ESLint 可能有助于适当的项目组织。

