用于构build/testing的Node.js项目布局与部署

我正在开发我的第一个Cloud Foundry项目(…和第一个Node.js项目,第一个MongoDB项目,第一个“express”项目等等)

在第一天,我发现了这个问题,并把答案作为组织我的github仓库的起点:

Node.js项目的文件夹结构

有一个未检入的/node_modules目录,而是由npm install根据package.json文件中的规范自动创build的。 好吧,我做了那个文件。

(注意:在vmc push ,似乎没有通过push-to服务器检查package.json文件,似乎只是复制了node_modules目录,如果它不存在则什么都不做。必须在您的客户端上执行npm install然后push。)

我的应用程序中有一些基础知识,现在我正要开始着手testing和构build基础架构。 例如:我想要一个构build过程,将运行在我所有的JavaScript上。 有一个名为ready.js的持续集成库,看起来像一个最新的构build工具…

但是,在我的项目目录中,并且正在执行npm install ready.js感觉有些不妥。 这意味着更多的东西将进入/node_modules目录并上传到云,当它不打算在云上运行。 同样道理:如果我有一个构build过程正在缩小资源(或其他任何),那么我不希望将源代码与vmc push部署。

我知道所有这些都是新的…但是有没有一个约定将目标转储到构build目录并从那里推送? 还是每个人都推动什么是有效的github根,并推动所有的构build和testing呢? 任何提示,欢迎…方法使用,避免方法…

更新 :我发现一个应用程序样板使用express和Node.js(以及其他几个常见的模块),它的服务器代码的JavaScript内部的“构build过程”…好或坏:

https://github.com/mape/node-express-boilerplate

我也发现了这一点,看起来像将“样板”这个术语与希望看到的模块名称结合到一起是一个很好的search策略,可以find我正在寻找的东西:

https://github.com/swbiggart/node-express-requirejs-backbone

npm允许你指定devDependencies ,你可能想看这篇文章 。

您可以在devDependencies下添加所有dev / test环境依赖devDependencies并在依赖项下添加所有与生产相关的模块。 然后,您可以添加一个脚本来推送到云。

我不熟悉Cloud Foundry或vmc push工作stream程。 但是,您可以将自定义脚本添加到package.json中scripts对象中,该对象安装dev-environment模块,运行testing,清除npmcaching,然后安装仅生产模块,并将代码以及仅将这些模块推送到云中。

编辑

我不确定你可以使用这些如果不推送到npm存储库,但它们是有用的作为一个例子(我猜…)或者,你可以自动化我在上面描述的工作stream脚本或节点脚本。

/编辑

您可以勾选任何可用的脚本…(更多信息,请参阅man npm-scripts ):

  preinstall Run BEFORE the package is installed install, postinstall Run AFTER the package is installed. preuninstall, uninstall Run BEFORE the package is uninstalled. postuninstall Run AFTER the package is uninstalled. preupdate Run BEFORE the package is updated with the update command. update, postupdate Run AFTER the package is updated with the update command. prepublish Run BEFORE the package is published. publish, postpublish Run AFTER the package is published. pretest, test, posttest Run by the npm test command. prestop, stop, poststop Run by the npm stop command. prestart, start, poststart Run by the npm start command. prerestart, restart, postrestart Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided. Additionally, arbitrary scrips can be run by doing npm run-script <stage> <pkg>. 

请注意,这里publish是为了将模块发布到npm 。 你应该把你的包设置为private( "private": true ),这样你不会意外地把代码发布到npm仓库中。