Deploy Nodejs in Production

2023-10-28 22:38
文章标签 nodejs deploy production

本文主要是介绍Deploy Nodejs in Production,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.

这篇关于Deploy Nodejs in Production的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/296421

相关文章

安装nodejs环境

本文介绍了如何通过nvm(NodeVersionManager)安装和管理Node.js及npm的不同版本,包括下载安装脚本、检查版本并安装特定版本的方法。 1、安装nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash 2、查看nvm版本 nvm --version 3、安装

nvm及nodejs安装相关

安装 1.清空文件夹,卸载nvm及nodejs 2.下载安装包 https://github.com/coreybutler/nvm-windows/releases (也下载有) 3.安装nvm 地址写D:/nvm和D:/nodejs 4.安装nodejs nvm ls available //查询版本nvm install 16.20.2 //安装对应版本号nvm use 1

Nodejs的Express框架使用总结

初始安装express,你也可以通过编辑器的shell安装依赖,比如vs # 创建并切换到 myapp 目录mkdir myappcd myapp# 初始化 package.json 文件npm init -y# 安装 express 到项目中npm i express 新建一个基础的接口 // 0. 加载 Expressconst express = require('expre

YApi 接口管理开源工具 nodejs+ mongoDB

前言介绍 Yapi 由 YMFE 开源,旨在为开发、产品、测试人员提供更优雅的接口管理服务,可以帮助开发者轻松创建、发布、维护 API。 权限管理 YApi 成熟的团队管理扁平化项目权限配置满足各类企业的需求 可视化接口管理 基于 websocket 的多人协作接口编辑功能和类 postman 测试工具,让多人协作成倍提升开发效率 Mock Server 易用的 Mock Server,

linux 安装 nodejs

1、去官网下载和自己系统匹配的文件:  英文网址:https://nodejs.org/en/download/  中文网址:http://nodejs.cn/download/  通过  uname -a  命令查看到我的Linux系统位数是64位(备注:x86_64表示64位系统, i686 i386表示32位系统),如图 故下载一下红色框中文件 ,版本为v6.10.0 2、

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

【NodeJS】如何安装淘宝cnpm

工具官网 Node.js淘宝 NPM 镜像 安装cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 输入cnpm -v查看是否正常(前提是你已经设置好了环境变量) cnpm -v 如果想给npm添加淘宝镜像,请参考: 【NodeJS】修改npm的registry为淘宝镜像(npm.taobao.o

【NodeJS】Unexpected token (109:0) 返回错误码500

刚开始报错是这样的: Unexpected token call 是什么我没看懂,但我发现 span.label.lable-success 后面的 #[i+1] 写错了,应该是 #{i+1} 改成完这个错误后又是一个错误提示: What? Unexpected token (109:0) 返回错误码500是什么鬼 我先将自己这段源码的 - if ... - else 检查下

怎么利用NodeJS发送视频短信

随着5G时代的来临,企业的数字化转型步伐日益加快,视频短信作为新兴的数字营销工具,正逐步展现出其大的潜力。视频群发短信以其独特的形式和内容,将图片、文字、视频、声音融为一体,为用户带来全新的直观感受,为企业营销注入新的活力。 支持免费对接试用乐讯通PaaS平台 找好用的短信平台,选择乐讯通,短信群发|短信平台|群发短信软件|群发短信平台|乐讯通PaaS平台http://yun.loktong

Nodejs 串口通信 : websocket , serialport

最近在学习如何实现web页面和串口间通信,网页请求使用websocket,实现的基本功能如下: 1、基本需求:硬件:有两个信号灯(TLA-505-1T),一个485继电器(开关),电压转换器,工业触屏一体机。 2、原理图:                  3工作原理: 首先一体机中采用node开发服务器,网页请求采用websocket协议,直接放代码了: var express