[英]Folder structure for a Node.js project
I notice that Node.js projects often include folders like these:我注意到 Node.js 项目通常包含以下文件夹:
/libs, /vendor, /support, /spec, /tests
/libs, /vendor, /support, /spec, /tests
What exactly do these mean?这些究竟是什么意思? What's the different between them, and where should I include referenced code?
它们之间有什么不同,我应该在哪里包含引用的代码?
Concerning the folders you mentioned:关于你提到的文件夹:
/libs
is usually used for custom classes/functions/modules
/libs
通常用于自定义classes/functions/modules
/vendor
or /support
contains 3rd party libraries (added as git sub-module when using git as source control) /vendor
或/support
包含第 3 方库(使用 git 作为源代码控制时添加为 git 子模块)/spec
contains specifications for BDD tests. /spec
包含 BDD 测试的/spec
。/tests
contains the unit-tests for an application (using a testing framework, see here ) /tests
包含应用程序的单元测试(使用测试框架,请参见此处) NOTE: both /vendor
and /support
are deprecated since NPM introduced a clean package management.注意:
/vendor
和/support
都已弃用,因为 NPM 引入了干净的包管理。 It's recommended to handle all 3rd-party dependencies using NPM and a package.json file建议使用 NPM 和 package.json 文件处理所有 3rd-party 依赖项
When building a rather large application, I recommend the following additional folders (especially if you are using some kind of MVC- / ORM-Framework like express or mongoose ):在构建一个相当大的应用程序时,我推荐以下附加文件夹(特别是如果您使用某种 MVC-/ORM-Framework,如express或mongoose ):
/models
contains all your ORM models (called Schemas
in mongoose) /models
包含您所有的 ORM 模型(在猫鼬中称为Schemas
)/views
contains your view-templates (using any templating language supported in express) /views
包含您的视图模板(使用 express 支持的任何模板语言)/public
contains all static content (images, style-sheets, client-side JavaScript) /public
包含所有静态内容(图像、样式表、客户端 JavaScript)
/assets/images
contains image files /assets/images
包含图像文件/assets/pdf
contains static pdf files /assets/pdf
包含静态 pdf 文件/css
contains style sheets (or compiled output by a css engine) /css
包含样式表(或由 css 引擎编译的输出)/js
contains client side JavaScript /js
包含客户端 JavaScript/controllers
contain all your express routes, separated by module/area of your application (note: when using the bootstrapping functionality of express, this folder is called /routes
) /controllers
包含您所有的 express 路由,由应用程序的模块/区域分隔(注意:使用 express 的引导功能时,此文件夹称为/routes
) I got used to organize my projects this way and i think it works out pretty well.我习惯于以这种方式组织我的项目,我认为它很有效。
Update for CoffeeScript-based Express applications (using connect-assets ):更新基于 CoffeeScript 的 Express 应用程序(使用connect-assets ):
/app
contains your compiled JavaScript /app
包含你编译的 JavaScript/assets/
contains all client-side assets that require compilation /assets/
包含所有需要编译的客户端资产
/assets/js
contains your client-side CoffeeScript files /assets/js
包含您的客户端 CoffeeScript 文件/assets/css
contains all your LESS/Stylus style-sheets /assets/css
包含你所有的 LESS/Stylus 样式表/public/(js|css|img)
contains your static files that are not handled by any compilers /public/(js|css|img)
包含没有被任何编译器处理的静态文件/src
contains all your server-side specific CoffeeScript files /src
包含所有服务器端特定的 CoffeeScript 文件/test
contains all unit testing scripts (implemented using a testing-framework of your choice) /test
包含所有单元测试脚本(使用您选择的测试框架实现)/views
contains all your express views (be it jade, ejs or any other templating engine) /views
包含您所有的表达视图(无论是 jade、ejs 还是任何其他模板引擎)There is a discussion on GitHub because of a question similar to this one: https://gist.github.com/1398757 GitHub上有一个讨论,因为一个类似的问题: https : //gist.github.com/1398757
You can use other projects for guidance, search in GitHub for:您可以使用其他项目作为指导,在 GitHub 中搜索:
And finally, in a book ( http://shop.oreilly.com/product/0636920025344.do ) suggests this structure:最后,在一本书( http://shop.oreilly.com/product/0636920025344.do )中提出了这种结构:
├── index.html
├── js/
│ ├── main.js
│ ├── models/
│ ├── views/
│ ├── collections/
│ ├── templates/
│ └── libs/
│ ├── backbone/
│ ├── underscore/
│ └── ...
├── css/
└── ...
More example from my project architecture you can see here:您可以在此处查看我的项目架构中的更多示例:
├── Dockerfile
├── README.md
├── config
│ └── production.json
├── package.json
├── schema
│ ├── create-db.sh
│ ├── db.sql
├── scripts
│ └── deploy-production.sh
├── src
│ ├── app -> Containes API routes
│ ├── db -> DB Models (ORM)
│ └── server.js -> the Server initlializer.
└── test
Basically, the logical app separated to DB and APP folders inside the SRC dir.基本上,逻辑应用程序分离到 SRC 目录内的 DB 和 APP 文件夹。
Assuming we are talking about web applications and building APIs:假设我们正在谈论 Web 应用程序和构建 API:
One approach is to categorize files by feature , much like what a micro service architecture would look like.一种方法是按功能对文件进行分类,就像微服务架构的样子。 The biggest win in my opinion is that it is super easy to see which files relate to a feature of the application.
在我看来,最大的好处是可以非常容易地查看哪些文件与应用程序的功能相关。
The best way to illustrate is through an example:最好的说明方法是通过一个例子:
We are developing a library application.我们正在开发一个图书馆应用程序。 In the first version of the application, a user can:
在应用程序的第一个版本中,用户可以:
In a second version, users can also:在第二个版本中,用户还可以:
In a third version, users can also:在第三个版本中,用户还可以:
First we have the following structure:首先我们有以下结构:
books
├─ controllers
│ ├─ booksController.js
│ └─ authorsController.js
│
└─ entities
├─ book.js
└─ author.js
We then add on the user and loan features:然后我们添加用户和贷款功能:
user
├─ controllers
│ └─ userController.js
├─ entities
│ └─ user.js
└─ middleware
└─ authentication.js
loan
├─ controllers
│ └─ loanController.js
└─ entities
└─ loan.js
And then the favorites functionality:然后是收藏夹功能:
favorites
├─ controllers
│ └─ favoritesController.js
└─ entities
└─ favorite.js
For any new developer that gets handed the task to add on that the books search should also return information if any book have been marked as favorite, it's really easy to see where in the code he/she should look.对于任何接受添加任务的新开发人员来说,如果有任何书籍被标记为最喜欢的,书籍搜索也应该返回信息,很容易看到他/她应该在代码中查看的位置。
Then when the product owner sweeps in and exclaims that the favorites feature should be removed completely, it's easy to remove it.然后,当产品负责人冲进来并大声说应该完全删除收藏夹功能时,删除它很容易。
It's important to note that there's no consensus on what's the best approach and related frameworks in general do not enforce nor reward certain structures.重要的是要注意,对于什么是最佳方法并没有达成共识,相关框架一般不会强制执行或奖励某些结构。
I find this to be a frustrating and huge overhead but equally important.我发现这是一个令人沮丧和巨大的开销,但同样重要。 It is sort of a downplayed version (but IMO more important) of the style guide issue .
这是style guide issue的轻描淡写版本(但 IMO 更重要)。 I like to point this out because the answer is the same: it doesn't matter what structure you use as long as it's well defined and coherent .
我想指出这一点,因为答案是一样的:只要定义明确且连贯,使用什么结构并不重要。
So I'd propose to look for a comprehensive guide that you like and make it clear that the project is based on this.因此,我建议寻找您喜欢的综合指南,并明确说明该项目基于此。
It's not easy, especially if you're new to this!这并不容易,特别是如果你是新手! Expect to spend hours researching.
预计花费数小时进行研究。 You'll find most guides recommending an MVC-like structure.
您会发现大多数指南都推荐了类似 MVC 的结构。 While several years ago that might have been a solid choice, nowadays that's not necessarily the case.
虽然几年前这可能是一个可靠的选择,但现在情况并非如此。 For example here's another approach .
例如, 这是另一种方法。
This is indirect answer, on the folder structure itself, very related.这是间接答案,与文件夹结构本身非常相关。
A few years ago I had same question, took a folder structure but had to do a lot directory moving later on, because the folder was meant for a different purpose than that I have read on internet, that is, what a particular folder does has different meanings for different people on some folders.几年前我有同样的问题,采用了一个文件夹结构,但后来不得不做很多目录移动,因为该文件夹的目的与我在互联网上阅读的目的不同,即特定文件夹的作用不同的人在某些文件夹上有不同的含义。
Now, having done multiple projects, in addition to explanation in all other answers, on the folder structure itself, I would strongly suggest to follow the structure of Node.js itself, which can be seen at: https://github.com/nodejs/node .现在,做了多个项目,除了所有其他答案中的解释,关于文件夹结构本身,我强烈建议遵循 Node.js 本身的结构,可以在以下位置看到: https : //github.com/节点/节点。 It has great detail on all, say linters and others, what file and folder structure they have and where.
它非常详细地介绍了所有内容,例如 linters 和其他人,他们拥有什么文件和文件夹结构以及在哪里。 Some folders have a README that explains what is in that folder.
某些文件夹有一个 README,解释了该文件夹中的内容。
Starting in above structure is good because some day a new requirement comes in and but you will have a scope to improve as it is already followed by Node.js itself which is maintained over many years now.从上述结构开始是好的,因为总有一天会出现新的需求,但是您将有改进的余地,因为 Node.js 本身已经遵循了它,并且已经维护了很多年。
Hope this helps.希望这可以帮助。
Just clone the repo from GitHub只需从 GitHub 克隆 repo
https://github.com/abhinavkallungal/Express-Folder-Structurehttps://github.com/abhinavkallungal/Express-Folder-Structure
this is a basic structure of a node.js express.js project with already setup MongoDB as database, hbs as view engine, nodemon also, so you can easily set up node js express project这是 node.js express.js 项目的基本结构,已经设置了 MongoDB 作为数据库,hbs 作为视图引擎,还有 nodemon,因此您可以轻松设置 node js express 项目
step 1: download or clone the repo step 2: Open in any code editor step 3: Open the terminal on the folder path step 4: run the comment in terminal npm start
step 5: start coding第 1 步:下载或克隆 repo 第 2 步:在任何代码编辑器中打开 第 3 步:在文件夹路径上打开终端 第 4 步:在终端
npm start
运行注释第 5 步:开始编码
happy Coding快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.