Node.js项目的文件夹结构

node.js Node.js

Jim古一

2020-03-18

我注意到Node.js项目通常包含以下文件夹:

/ libs,/ vendor,/ support,/ spec,/ tests

这些到底是什么意思?它们之间有什么区别,我应该在哪里包含引用的代码?

第2066篇《Node.js项目的文件夹结构》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

5个回答
TomEva 2020.03.18

GitHub上存在一个讨论,因为存在与此类似的问题:https : //gist.github.com/1398757

您可以使用其他项目作为指导,在GitHub中搜索:

  • ThreeNodes.js –在我看来,似乎具有不适合每个项目的特定结构;
  • 更轻-更简单的结构,但缺乏组织性;

最后,在书中(http://shop.oreilly.com/product/0636920025344.do)提出了以下结构:

├── index.html
├── js/
│   ├── main.js
│   ├── models/
│   ├── views/
│   ├── collections/
│   ├── templates/
│   └── libs/
│       ├── backbone/
│       ├── underscore/
│       └── ...
├── css/
└── ...
JinJin小卤蛋 2020.03.18

这是间接答案,关于文件夹结构本身,非常相关。

几年前,我有一个相同的问题,采用了文件夹结构,但后来不得不做很多目录移动,因为该文件夹的目的与我在互联网上阅读的目的不同,即特定文件夹的功能不同人在某些文件夹上的含义不同。

现在,除了对所有其他答案进行解释之外,在文件夹结构本身上做了多个项目,我强烈建议遵循Node.js本身的结构,该结构可以在以下网址查看:https : //github.com/ nodejs / node它详细介绍了所有内容,例如linter和其他文件,它们具有什么文件和文件夹结构以及位置。有些文件夹具有自述文件,说明该文件夹中的内容。

从上面的结构开始是一个好习惯,因为有一天会有新的需求出现,但是您将有一个改进的余地,因为Node.js本身已经被它所遵循,并且已经维护了很多年。

希望这可以帮助。

Green小小 2020.03.18

我的项目架构中的更多示例可以在这里看到:

├── 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

基本上,逻辑应用程序分离到SRC目录中的DB和APP文件夹。

理查德卡卡西 2020.03.18

关于您提到的文件夹:

  • /libs 通常用于自定义 classes/functions/modules
  • /vendor/support包含第三方库(使用git作为源代码管理时添加为git子模块)
  • /spec 包含BDD测试规范。
  • /tests包含应用程序的单元测试(使用测试框架,请参见 此处

注意:自NPM引入了干净的程序包管理以来,/vendor/support都已弃用。建议使用NPM和package.json文件处理所有第三方依赖关系

在构建较大的应用程序时,我建议使用以下附加文件夹(尤其是在使用某种MVC- / ORM-Framework(例如expressmongoose)时):

  • /models包含您所有的ORM模型(Schemas在猫鼬中称为
  • /views contains your view-templates (using any templating language supported in express)
  • /public contains all static content (images, style-sheets, client-side JavaScript)
    • /assets/images contains image files
    • /assets/pdf contains static pdf files
    • /css contains style sheets (or compiled output by a css engine)
    • /js contains client side 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)

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):

  • /app contains your compiled JavaScript
  • /assets/ contains all client-side assets that require compilation
    • /assets/js contains your client-side CoffeeScript files
    • /assets/css contains all your LESS/Stylus style-sheets
  • /public/(js|css|img) contains your static files that are not handled by any compilers
  • /src contains all your server-side specific CoffeeScript files
  • /test contains all unit testing scripts (implemented using a testing-framework of your choice)
  • /views contains all your express views (be it jade, ejs or any other templating engine)
樱神奇 2020.03.18

重要的是要指出,就什么是最好的方法尚无共识,并且相关框架通常不会强制执行或奖励某些结构。

我发现这是令人沮丧的巨大开销,但同样重要。它是样式指南问题的低调版本(但IMO更重要)我想指出这一点,因为答案是相同的:只要定义明确且连贯,使用哪种结构都没有关系

因此,我建议寻找您喜欢的综合指南,并明确说明该项目基于此。

这并不容易,特别是如果您是新手!期望花费数小时进行研究。您会发现大多数指南都推荐类似MVC的结构。尽管几年前这可能是一个不错的选择,但如今并不一定如此。例如,这是另一种方法

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android