简体   繁体   English

nodejs express 应用程序部署到生产

[英]nodejs express app deploying to production

Sorry if this is a basic question, I am still wrapping my head around nodejs deployments.对不起,如果这是一个基本问题,我仍然在围绕 nodejs 部署。 I have an app written on nodejs which I want to deploy to production.我有一个在 nodejs 上编写的应用程序,我想将其部署到生产环境中。 So far for testing purposes I have used Express.到目前为止,出于测试目的,我使用了 Express。 However from what I know Express is a dev server and not a production server.但是,据我所知,Express 是开发服务器而不是生产服务器。 How do I deploy the nodejs app to production and what is the server I should use.如何将 nodejs 应用程序部署到生产环境以及我应该使用什么服务器。 Plus I have a lot of code written for express like routes and middleware, how does this work when I deploy to another server?另外,我有很多为 express 编写的代码,比如路由和中间件,当我部署到另一台服务器时它是如何工作的?

When you create your application with express.js, all express modules have been specified in package.json and will be installed as npm modules.当您使用 express.js 创建应用程序时,所有 express 模块都已在 package.json 中指定,并将作为 npm 模块安装。

All you need to do is just install node.js on your production servers, put your code there, run npm install , then start the web server with NODE_ENV=production param.你需要做的就是在你的生产服务器上安装 node.js,把你的代码放在那里,运行npm install ,然后用NODE_ENV=production参数启动 web 服务器。 It would be a plus if you can use grunt or gulp to process static assets (js / css minification, ...) to optimize the performance for production mode.如果您可以使用 grunt 或 gulp 处理静态资产(js / css 缩小,...)以优化生产模式的性能,那将是一个加分项。

For more information, you can take a look here: https://expressjs.com/en/advanced/best-practice-performance.html有关更多信息,您可以在这里查看: https : //expressjs.com/en/advanced/best-practice-performance.html

You can use pm2 combined with ExpressJs to deploy on remote server.您可以使用pm2结合ExpressJs部署在远程服务器上。 Basically pm2 makes your code run forever in background task with cluster mode.基本上 pm2 使您的代码在集群模式的后台任务中永远运行。

Basically you need to put your files into remote server and create a .json or yaml file for pm2 to understand what will be the process.基本上,您需要将文件放入远程服务器并为 pm2 创建一个 .json 或 yaml 文件以了解该过程。

Then you should install libs and modules through npm install .然后你应该通过npm install安装库和模块。 After this you need to install pm2 on your remote server using npm i -g pm2 .在此之后,您需要使用npm i -g pm2在远程服务器上安装npm i -g pm2

After all of them, you can use pm2's basic cli commands to deploy.毕竟,您可以使用 pm2 的基本 cli 命令进行部署。 pm2 start process.yml --env=production is a basic example for deployment. pm2 start process.yml --env=production是一个基本的部署示例。 You can use pm2 stop process.yml for stopping the task.您可以使用pm2 stop process.yml来停止任务。 You can also use pm2 monit for system monitoring.您还可以使用pm2 monit进行系统监控。 If you want to see the logs, you can run pm2 logs .如果要查看日志,可以运行pm2 logs If you want to restart or update: pm2 reload all or pm2 update helps.如果要重新启动或更新: pm2 reload allpm2 update帮助。

For remote server, you can use clouds for best performance.对于远程服务器,您可以使用云以获得最佳性能。 You can look the pricing of AWS , Azure , GCloud , Heroku or DigitalOcean .您可以查看AWSAzureGCloudHerokuDigitalOcean的定价。 They are providing free tiers and you can search for them.他们提供免费层,您可以搜索它们。

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

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