Come from: https://codeforgeek.com/2016/09/10-things-running-node-js-production/
1: Use reverse proxy
“Listening to port 3000”
I am pretty sure you have come across this common message in your terminal while running Node.js application. This works great in local development because hitting localhost:3000 from a browser is no big deal.
But…What about production? I assume you can’t ask your user to visit your web app by accessing port.
But wait! We can run our app on Port 80 and browser will automatically understand that.
True, but this way you will lose the greatest flexibility and that is running multiple nodejs apps on the same server.
Plus, you will expose your real Port on which your App is running i.e it is directly accessible and open to everyone. This is too risky.
You can avoid all of this by introducing reverse proxy in your setup. This will not only hide the real app configuration but also allows your to run multiple apps on same but on the different port.
Plus you can disallow the port for outside user on which your App is listening and tighten the security.
Nginx is still the king in reverse proxy Server. You can use that, however you can also develop reverse proxy Server in Node.js. We have covered this here.
2: Use monitoring tools
Exceptions happen and Server does crash. Whenever it happens, you should be the one who gets notified, probably not by your Manager but your monitoring tool.
In production, a monitoring tool is your best friend, so choose them wisely. These tools allow you to check the status of your deployment from anywhere, you can check logs, memory usage and get the notification via an email if any exception happens.
Nodejs monitoring using PM2
I personally used two monitoring tools as of now.
Keymetrics – by PM2.
Trace – by RisingStack
I have covered tutorial on keymetrics, you can learn about it here.
3: Remove console.log statements
We love console.log and why shoud’t we.
It helps us to trace bugs and save our time.
But in production, these statements consumes CPU time and waste the resources. It’s better if you get rid of them in production.
Tip: Use good logging module which contains features such as async log writing, log rotation, and external log storage support. I have used winston module in one app and I can’t complain.
4: Using external store for global store
Storing session ids, sockets ids etc in memory as the temporary store is not gonna work in production because every time you restart your server which could be often in an early stage, you will loose your data. You must use fast external store such as Redis to store such data.
Nodejs and redis
We have covered using Redis as Session store in Node.js here.
5: Use SSL
SSL provides you an extra layer of security by encrypting the data transmission.
Now, most of the time, Nodejs developers reads the SSL key from the file in the Node Server itself, you should always do that using reverse proxy.
Install SSL on reverse proxy and let outside world communicate your Node app via reverse proxy.
6: Recheck security measures
Make sure your app is meeting the security standards. Our fellow developers at RisingStack compiles a list of security check you should consider.
Read the awesome Nodejs security checklist article
Also, always update Nodejs and NPM version to latest one, they contain a lot of security patch and updates.
7: Keep real app only accessible by private users
If your app is running on XYZ.com Server on Port 8080 and it has a reverse proxy, then make sure only reverse proxy Server can communicate to your app, no one else from outside world. This prevents lots of attacks and security loopholes.
Also, restrict the SSH access to the specific network or VPN to prevent the outside access.
8: Run Node.js in cluster mode
Node.js by default do not take advantages of multiple cores of the processor, hence throughput of the system is low if you do not deploy your app in cluster mode.
Node.js clustering
Node.js cluster module allows you to fork multiple child processes of same process. This allows zero downtime and high availaibiilty.
In production, always run your app in cluster mode.
9: Perform more I/O and less CPU intensive task
Always remember, Nodejs runs on single threaded event loop and it is best suited for I/O intensive operation.
Even if I/O operation takes time, Nodejs will serve other requests because of callback nature, however, if your application is taking too much time in performing CPU task such as big calculation etc then Nodejs will be held up the other request and this will in turn slow down the system.
Try to use Node for more I/O operation than processing operation.
10: Use process managers
Running your app with node app.js or gulp etc is fine in development stage. The minute you move to production, you must use process managers to keep your app running forever. These process managers help you to restart your application in case there is an exception.
PM2 application server monitoring
There are npm modules such as forever, pm2 to handle process. We recommend PM2, because of its ease of use and keymetric integration.
Summary
I have covered ten important tips that we should look over during the production deployment. There could be more specific to projects but these ones I found to be general.
分享到:
相关推荐
为客户定制春节集福活动系统, 支持高并发、分布部署。基于 Nodejs\MySql\Redis\Traefix\NuxtJS 等技术。线上线下结合,拉新与落地一气...Use pm2 to deploy app on production enviroment. pm2 startOrReload pm2.json
【权限管理系统】基于react+antd4+typeScript+nodeJs的RBAC基于角色的权限控制管理系统.zip QuickStart Development npm i npm install npm run dev-server npm run dev-client Production npm run build-...
为客户定制春节集福活动系统, 支持高并发、分布部署。基于 Nodejs\MySql\Redis\Traefix\NuxtJS 等技术。线上线下结合,拉新与落地一气...Use pm2 to deploy app on production enviroment. pm2 startOrReload pm2.json
linked with examples of how to deploy and use those with a bit of NodeJS and Python example code, so that a reader could quickly replicate and use that knowledge themselves in their development ...
This reporitory is for deploy j2ee app to test/production environment 涉及技术关键字: linux , tomcat , daemontools , bash , jolokia bin dev test production三个环境配置分离。 jstart是tomcat启动脚本 ...
自动化部署:1、先提交到git2、使用pm2进行部署:pm2 deploy ecosystem.json production或者直接:npm run deploy接口说明:基础接口1、域名:http://wxnode.xiaoxiekeke.com/2、接入微信:...```...
vimeo-up-lambda AWS Lambda函数,可使用Vimeo API将视频上传到Vimeo 先决条件 Nodejs 设置 ...then, reset production to master and manually deploy to production branch stage/environment notes
当配置完成后,可以使用PM2的`deploy`子命令来执行部署流程。通过设定的部署策略(如`git pull`),PM2会拉取远程仓库的最新代码,然后重启应用,确保新代码生效。 以下是基本的PM2部署命令: 1. 配置部署环境: ...
"heroku-postbuild": "npm install --only=production" } ``` 这将在Heroku环境中启动你的应用,并且只在部署时安装生产依赖。 现在,我们需要一个MongoDB的Heroku附加组件。在Heroku CLI中,登录并使用以下命令...
npm run pm2 --env=production 使用PM2启动生产服务器 npm run deploy:heroku 更新Heroku应用 npm run deploy:server 将应用程序部署到服务器 npm run gulp 启动浏览器同步和骚扰 npm run webpack 启动webpack ...
select which stages to deploy alarms to - production - staging dashboards : true nameTemplate : $[functionName]-$[metricName]-Alarm # Optionally - naming template for alarms, can be overwritten in ...
- name: Deploy to Production (if all previous steps succeed) if: ${{ success() }} # Add deployment steps here ``` 以上就是关于"github-actions-typescript-nodejs-ci"主题的主要知识点,包括GitHub ...
运行`serverless deploy`命令,Serverless Framework会将代码上传到S3,创建Lambda函数和API Gateway,并完成部署。 **五、监控和日志** 对于生产环境,监控和日志非常重要。可以使用如Dashbird这样的服务来监控...
If you already know the basics of Node.js, now is the time to discover how to bring it to production level by leveraging its vast ecosystem of packages.With this book, you'll work with a varied ...
分行master :master分支用于制定规则,并在上自动部署production :/!\生产分支用于部署到生产Auth0租户。 从生产分支到生产租户的部署是由手动执行CodeBuild作业触发的部署和CI CI完成后,使用auth0-ci 部署规则...
汉阳大学埃里卡校园聊天机器人 先决条件 [Node.js] [nodejs-url]> = 10.18.1 [NPM] [npm-url]> = 6.13.4 > = 8 Gradle-如果您没有请访问并获取它。...# mode: production - production 상태(deploy 버전) np
sudo yum install -y nodejs npm install -g @vue/cli ``` 4. **项目结构**: 假设你的Vue项目位于`frontend`目录,Django项目位于`backend`目录。在各自的目录下,管理项目文件和设置虚拟环境(可选,但推荐)...
管理员 描述 RBAC基于角色权限控制管理系统 快速开始 发展历程 npm i npm install npm run dev - server ...pm2 deploy ecosystem . json production setup pm2 deploy ecosystem . json production