简体   繁体   English

如何安装和部署node.js应用程序?

[英]How to install and deploy node.js application?

I'm new to node.js. 我是node.js的新手。 After creating modularized project with express, tests, .nvmrc etc. it's finally time to deploy the app. 在使用express,测试, .nvmrc等创建模块化项目之后,终于可以部署该应用了。 How it should be done? 应该怎么做? in java you bundle your project into a single file, self containing and you put in into a server with some configuration. 在Java中,您将项目捆绑到一个单独的文件中,该文件包含自包含文件,并以某种配置将其放入服务器中。 what about node.js? 那node.js呢?

Should i just copy the whole directory with sources and node_modules to production machine and use systemd, pm2 or other process manager to just run it? 我是否应该将带有源代码和node_modules的整个目录复制到生产机器,并使用systemd,pm2或其他进程管理器来运行它? but i heard some of the dependencies might be system-dependend so they may work incorrectly 但我听说某些依赖项可能是系统依赖项,因此它们可能无法正常工作

or should i copy only sources and run npm install --production on the production machine? 还是应该仅复制源并在生产计算机上运行npm install --production but this way the deployment is only possible when npm repositories are online. 但是这种方式只有在npm存储库在线时才可以部署。 also it takes time to build the application and it has to be done on all machines in the cluster. 此外,构建应用程序还需要花费时间,并且必须在集群中的所有计算机上完成。 also what about quickly rolling back to previous version in case there is some bug? 如果出现错误,如何快速回滚到以前的版本呢? again, time and online npm repos are needed 再次,需要时间和在线npm仓库

another option is to build a docker image. 另一个选择是构建docker映像。 but it seems awkward that the only way to easily and safely deploy the app is using third party technology 但是似乎很尴尬的是,轻松,安全地部署应用程序的唯一方法是使用第三方技术

how it's being done in real life scenarios? 在现实生活中如何进行?

sure don't copy the whole directory especially node_modules. 确保不要复制整个目录,尤其是node_modules。

all the packages installed on your system should be installed with --save option example: npm install --save express if you do so you will have in your package.json the dependencies required for your project whether they are dev dependencies or production dependencies. 系统上安装的所有软件包都应使用--save选项示例进行安装: npm install --save express如果这样做),则在package.json中将具有项目所需的依赖关系,无论它们是开发依赖关系还是生产依赖关系。

I don't know what your project structure looks like, but as a node application you have to run npm init . 我不知道您的项目结构是什么样子,但是作为节点应用程序,您必须运行npm init . in your project to setup the package.json file and then you can start adding your dependencies with --save . 在项目中设置package.json文件,然后可以使用--save开始添加依赖项。

usually we use git 通常我们使用git

version control system 版本控制系统

to deploy to the server, first we push our code to a git repository then we pull from it to the server git 要部署到服务器,首先我们将代码推送到git存储库,然后将其从其拉到服务器git

you have to add .gitignore in your project and ignore node_modules from being committed to your git repository. 您必须在项目中添加.gitignore,并忽略将node_modules提交到git存储库中。

then you can pull to your server and run npm install on the server. 然后您可以拉到服务器并在服务器上运行npm install and sure you need to launch your web server to serve your application example ngnix 并确保您需要启动Web服务器来服务您的应用程序示例ngnix

you can try Heroku for an easy deployment, all you have to do is to setup your project with Heroku, and when you push your code, Heroku manages the deployment . 您可以尝试使用Heroku进行简单的部署,您要做的就是使用Heroku设置项目,并在推送代码时由Heroku管理部署。 Heroku Heroku

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM