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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21259181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:43:33  来源:igfitidea点击:

How to package & deploy Node.js + express web application?

node.jsexpressweb-deploymentpackaging

提问by KBJ

I am new to Node.js programming and I have recently created a sample working web application using (express, backbone & other complimentary view technologies, with mongoDB). Now i am at a point where I want to deploy the same on a staging environment and I am not sure how to package this application and distribute the same. [I can take care of mongoDb and setting it up seperately]

我是 Node.js 编程的新手,最近我使用(express、backbone 和其他免费视图技术,使用 mongoDB)创建了一个示例工作 Web 应用程序。现在我想在登台环境中部署相同的应用程序,但我不确定如何打包和分发该应用程序。[我可以照顾mongoDb并单独设置]

I am from Java world and in there we create jars for reusable libs and war/ear packages for web applications which is deployed in a servlet container. Now in this case since node.js itself acts as a web container as well, how do i package my webapp?

我来自 Java 世界,在那里我们为部署在 servlet 容器中的 Web 应用程序创建可重用库和 war/ear 包的 jar。现在在这种情况下,由于 node.js 本身也充当 Web 容器,我该如何打包我的 Web 应用程序?

  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?)
  2. How do I deploy it once packaged? Would it become an exe, since it is also its own container?
  1. 是否有使用 express 构建的打包节点 webapps 的任何标准格式/指南?(节点应用程序是否有类似的 jar/war 打包系统?)
  2. 打包后如何部署?它会成为一个exe,因为它也是它自己的容器吗?

PS: As of now I am thinking of just manually copying all the required source files into the staging environment and run npm commands to download all dependencies on that machine and then use 'forever' or some other mechanism to run my server.js. (Also, add some sort of monitoring, just in case app crashes and forever fails) I am not sure if that is the right way? I am sure there must be some standardized way of addressing this problem.

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

采纳答案by MildlySerious

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

没有标准化的方法,但您走在正确的轨道上。如果您package.json是最新的并且保存完好,您只需将您的应用程序目录复制/压缩/克隆到生产系统,不包括node_modules.

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

在您的生产系统上,运行 npm install以安装您的依赖项,npm test如果您有测试,最后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.

我认为最近的一些幻灯片非常有帮助,其中还包括像永远这样的包装器主题,可以在这里找到。

回答by Hüseyin BABAL

Deploying Node.jsapplications is very easy stuff. In maven, there is pom.xml. Related concept in Node.jsis 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

部署Node.js应用程序是非常容易的事情。在 maven 中,有pom.xml. Node.jsis 中的相关概念package.json。您可以在package.json. 您还可以在 上进行环境设置package.json。例如,在开发环境中,您可以说

I want to run unit tests.

我想运行单元测试。

but in production;

但在生产中;

I want to skip unit tests.

我想跳过单元测试。

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

您在.m2文件夹下有 Maven 的本地存储库。在 Node.js 中,node_modules您的 Node.js 项目下有一个文件夹。您可以看到带有其名称的模块文件夹。

Let's come to the gruntpart of this answer. Gruntis 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 grunttask run functions in package.json.

让我们来看看grunt这个答案的部分。Grunt是您的前端资产、html、javascript、css 的任务管理器。例如,在部署之前,您可以缩小 html、css、javascript 甚至图像。您还可以将grunt任务运行函数放在package.json.

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

如果您想查看示例应用程序,可以在此处找到示例博客应用程序。检查文件夹结构package.json以供参考。

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

对于部署,我建议您为启动应用程序进行 heroku 部署。您可以在此处找到操作方法。这是简单的基于 git 的部署。

On project running part, simply set your environment NODE_ENV=developmentand node app.js. Here app.jsis in your project.

在项目运行部分,只需设置您的环境NODE_ENV=developmentnode app.js. 这app.js是在您的项目中。

Here is relative concept for java and nodejs;

这是java和nodejs的相对概念;

  1. maven clean install=> npm install
  2. .m2folder => 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. maven clean install=> npm install
  2. .m2文件夹 => node_modules(在项目文件夹下)
  3. mvn test=> npm test(测试部分package.json
  4. junit, powermock, ... => mocha, node-unit, ...
  5. Spring MVC=> Express.JS
  6. pom.xml=> package.json
  7. import package=> require('module_name')

回答by David Braun

  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?)
  1. 是否有使用 express 构建的打包节点 webapps 的任何标准格式/指南?(节点应用程序是否有类似的 jar/war 打包系统?)

Yes, the CommonJS Packages specification:

是的,CommonJS 包规范

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.

本规范描述了用于分发 CommonJS 程序和库的 CommonJS 包格式。CommonJS 包是将模块、代码和其他资产的集合紧密地包装成单个形式。它为CommonJS组件的便捷交付、安装和管理提供了基础。

For your next question:

对于您的下一个问题:

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

2. 打包后如何部署?它会成为一个exe,因为它也是它自己的容器吗?

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

我支持 Hüseyin 的建议,即部署在Heroku 上进行生产。对于开发和登台,我分别将Node-Appliance与 VirtualBox 和 Amazon EC2 结合使用:

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.

该程序采用由 build-debian-cloud 或 Debian-VirtualBox-Appliance 构建的 Debian 机器,并将其转换为 Node.js “设备”,能够运行通过 git 部署的 Node 应用程序。

Your webapp will not become an exe.

您的 webapp 不会成为exe.

回答by naveen kumar

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.

希望这可能对寻找解决方案的人有所帮助,可以使用“npm pack”命令打包 Node js 应用程序。它会创建一个可以在生产/暂存环境中运行的应用程序的 zip 文件。

回答by dark_ruby

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 installto restore all dependencies

  • create an NPM package out of it , install it via npmin 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

  • 将您的代码推送到 Git 存储库中,排除不是您的代码的所有内容 ( node_modules/**),然后将其拉入您的暂存环境中,运行npm install以恢复所有依赖项

  • npm从中创建一个 NPM 包,通过在您的临时环境中安装它(这也应该处理所有依赖项)

  • 手动复制/ssh 文件到您的暂存环境(这可以通过 自动执行Grunt),而不是通过以下方式恢复您的依赖项npm

回答by varad11

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.

我使用了 zeit 的 pkg 模块。它可以为 linux/win/macos 创建跨平台交付物。实际上在生产中使用它并且工作正常,没有任何问题。

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

它接收所有 js 脚本并将其打包到一个文件中。

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.

另一个优点是,在生产环境中,您实际上并不需要让客户安装 node.js,因为节点二进制文件也会打包在构建中。

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

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