如何打包和部署 Node.js + Express Web 应用程序?

我是 Node.js 编程的新手,最近我使用 mongoDB 创建了一个可以工作的 Web 应用程序示例(Express、主干和其他免费的视图技术)。现在我希望在一个临时环境中部署相同的应用程序,但是我不确定如何打包这个应用程序并分发相同的应用程序。[我可以照顾 mongoDb 和设置它分开]

我来自 Java 世界,在那里我们为可重用的库和 web 应用程序的 war/ear 包创建 jar,它们被部署在一个 servlet 容器中。既然 node.js 本身也是一个 web 容器,那么我该如何打包我的 webapp 呢?

  1. 是否有任何标准格式/准则包装节点网络应用程序建立使用快递?(节点应用程序是否有类似的 jar/war 打包系统?)
  2. 打包后如何部署它? 它是否会变成 exe,因为它也是自己的容器?

PS: 到目前为止,我考虑的只是手动将所有必需的源文件复制到临时环境中,然后运行 npm 命令下载该机器上的所有依赖项,然后使用‘ forever’或其他机制运行 server.js。(另外,添加一些监控,以防应用程序崩溃并永远失败)我不确定这是否是正确的方法?我相信一定有某种标准化的方法来解决这个问题。

71553 次浏览

There is no standardized way, but you're on the right track. If your package.json is up to date and well kept, you can just copy/zip/clone your app directory to the production system, excluding the node_modules.

On your production system, run npm install to install your dependencies, npm test if you have tests and finally NODE_ENV=production node server.js

Some recent slides I considered to be quite helpful that also include the topic of wrappers like forever, can be found here.

few ways to approach this:

  • Push your code into Git repository, excluding everything that isn't your code (node_modules/**), then pull it in your staging environment, run npm install to restore all dependencies

  • create an NPM package out of it , install it via npm in your staging environment (this should also take care of all of the dependencies)

  • manual copy/ssh files to your staging environment (this can be automated with Grunt), than restore your dependencies via npm

Deploying Node.js applications is very easy stuff. In maven, there is pom.xml. Related concept in Node.js is package.json. You can state your dependencies on package.json. You can also do environmental setup on package.json. For example, in dev environment you can say that

I want to run unit tests.

but in production;

I want to skip unit tests.

You have local repositories for maven under .m2 folder. In Node.js, there is node_modules folder under your Node.js project. You can see module folders with its name.

Let's come to the grunt part of this answer. Grunt is a task manager for your frontend assets, html, javascript, css. For example, before deployment you can minify html, css, javascript even images. You can also put grunt task run functions in package.json.

If you want to look at a sample application, you can find an example blog application here. Check folder structure and package.json for reference.

For deployment, I suggest you heroku deployment for startup applciations. You can find howto here. This is simple git based deployment.

On project running part, simply set your environment NODE_ENV=development and node app.js. Here app.js is in your project.

Here is relative concept for java and nodejs;

  1. maven clean install => npm install
  2. .m2 folder => node_modules(Under project folder)
  3. mvn test => npm test(test section on package.json)
  4. junit, powermock, ... => mocha, node-unit, ...
  5. Spring MVC => Express.JS
  6. pom.xml => package.json
  7. import package => require('module_name')
  1. Is there any standard format/guidelines of packaging node webapps built using express? (Is there a similar jar/war packaging systems for node apps?)

Yes, the CommonJS Packages specification:

This specification describes the CommonJS package format for distributing CommonJS programs and libraries. A CommonJS package is a cohesive wrapping of a collection of modules, code and other assets into a single form. It provides the basis for convenient delivery, installation and management of CommonJS components.

For your next question:

2. How do I deploy it once packaged? Would it become an exe, since it is also its own container?

I second Hüseyin's suggestion to deploy on Heroku for production. For development and staging I use Node-Appliance with VirtualBox and Amazon EC2, respectively:

This program takes a Debian machine built by build-debian-cloud or Debian-VirtualBox-Appliance and turns it into a Node.js "appliance", capable of running a Node application deployed via git.

Your webapp will not become an exe.

Hope this might be helpful for somebody looking for the solution,Packaging of Node js apps can be done using "npm pack" command.It creates a zip file of your application which can be run in production/staging environment.

I used zeit's pkg module. It can create cross platform deliverables for linux/win/macos. Actually used it in production and works fine without any issues.

It takes in all the js scripts and packages it into a single file.

The reason I used it is because it helps in securing your source code. That way in production at customers environment they will have access to application but not the source code.

Also one of the advantages is that at production environment, you do not actually need to have the customer install node.js as the node binaries also get packaged inside the build.

https://www.npmjs.com/package/pkg