Node.js项目的文件夹结构 [英] Folder structure for a Node.js project

查看:599
本文介绍了Node.js项目的文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I notice that Node.js projects often include folders like these:

/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?

推荐答案

关于您提到的文件夹:

  • /libs 通常用于自定义classes/functions/modules
  • /vendor /support 包含第三方库(添加为git git作为源代码控制时的子模块)
  • /spec 包含BDD测试的规范.
  • /tests 包含应用程序的单元测试(使用测试 框架,请参阅 此处)
  • /libs is usually used for custom classes/functions/modules
  • /vendor or /support contains 3rd party libraries (added as git sub-module when using git as source control)
  • /spec contains specifications for BDD tests.
  • /tests contains the unit-tests for an application (using a testing framework, see here)

注意:由于NPM引入了干净的程序包管理,因此不推荐使用/vendor/support.建议使用NPM和package.json文件处理所有第三方依赖项

NOTE: both /vendor and /support are deprecated since NPM introduced a clean package management. It's recommended to handle all 3rd-party dependencies using NPM and a package.json file

在构建相当大的应用程序时,我建议使用以下其他文件夹(尤其是如果您使用的是诸如猫鼬):

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

  • /models 包含您的所有ORM模型(在猫鼬中称为Schemas)
  • /views 包含您的视图模板(使用express中支持的任何模板语言)
  • /public 包含所有静态内容(图像,样式表,客户端JavaScript)
    • /assets/images 包含图像文件
    • /assets/pdf 包含静态pdf文件
    • /css 包含样式表(或CSS引擎编译的输出)
    • /js 包含客户端JavaScript
    • /models contains all your ORM models (called Schemas in mongoose)
    • /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

      我已经习惯了以这种方式组织项目,我认为效果很好.

      I got used to organize my projects this way and i think it works out pretty well.

      基于CoffeeScript的Express应用程序的更新(使用连接资产):

      Update for CoffeeScript-based Express applications (using connect-assets):

      • /app 包含您已编译的JavaScript
      • /assets/ 包含所有需要编译的客户端资产
        • /assets/js 包含您的客户端CoffeeScript文件
        • /assets/css 包含您的所有LESS/Stylus样式表
        • /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

          这篇关于Node.js项目的文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆