Skip to main content

Session 03: Tools

Auto reload with nodemon

Nodemon là một công cụ hữu ích cho phát triển ứng dụng Node.js. Nó cho phép bạn tự động khởi động lại ứng dụng Node.js của mình khi có bất kỳ thay đổi nào trong mã nguồn của bạn.

Để sử dụng Nodemon, bạn cần cài đặt nó thông qua npm bằng lệnh sau:

npm install -g nodemon

Sau khi cài đặt xong, bạn có thể chạy ứng dụng của mình bằng cách thay thế lệnh node bằng nodemon, ví dụ:

nodemon app.js

Khi bạn lưu lại các thay đổi trong mã nguồn của mình, Nodemon sẽ tự động khởi động lại ứng dụng của bạn để áp dụng các thay đổi mới.

Ngoài ra, Nodemon cung cấp một số tùy chọn để tùy chỉnh việc chạy ứng dụng của bạn, bao gồm:

--delay: Chỉ định thời gian chờ giữa các lần khởi động lại. Mặc định là 1 giây.

--watch: Chỉ định các tệp tin hoặc thư mục mà Nodemon sẽ theo dõi để khởi động lại ứng dụng khi chúng được chỉnh sửa.

--ignore: Chỉ định các tệp tin hoặc thư mục mà Nodemon sẽ bỏ qua khi theo dõi thay đổi.

Ví dụ, để theo dõi các thay đổi trong thư mục src và bỏ qua các tệp tin có đuôi .test.js, bạn có thể chạy lệnh sau:

nodemon --watch src --ignore '*.test.js' app.js

Process management with PM2

https://expressjs.com/en/advanced/pm.html

A production process manager for Node.js applications that has a built-in load balancer. PM2 enables you to keep applications alive forever, reloads them without downtime, helps you to manage application logging, monitoring, and clustering.

https://github.com/Unitech/pm2

Starting an application in production mode is as easy as:

pm2 start app.js

PM2 is constantly assailed by more than 1800 tests.

Official website: https://pm2.keymetrics.io

Works on Linux (stable) & macOS (stable) & Windows (stable). All Node.js versions are supported starting Node.js 12.X.

Installing PM2 With NPM:

npm install pm2 -g

You can install Node.js easily with NVM or ASDF.

Start an application You can start any application (Node.js, Python, Ruby, binaries in $PATH...) like that:

pm2 start app.js

Your app is now daemonized, monitored and kept alive forever.

Managing Applications

Once applications are started you can manage them easily:

To list all running applications:

pm2 list

To have more details on a specific application:

pm2 describe <id|app_name>

To monitor logs, custom metrics, application information:

pm2 monit

Cluster Mode: Node.js Load Balancing & Zero Downtime Reload

The Cluster mode is a special mode when starting a Node.js application, it starts multiple processes and load-balance HTTP/TCP/UDP queries between them. This increase overall performance (by a factor of x10 on 16 cores machines) and reliability (faster socket re-balancing in case of unhandled errors).

Starting a Node.js application in cluster mode that will leverage all CPUs available:

pm2 start api.js -i <processes>

<processes> can be 'max', -1 (all cpu minus 1) or a specified number of instances to start.

Zero Downtime Reload Hot Reload allows to update an application without any downtime:

pm2 reload all