동기와 비동기
동기적 처리 : 작업을 순차적으로 처리(A작업이 끝난 후 B작업 수행)
비동기적 처리 : 작업을 병렬적으로 처리(A 작업이 끝나고 호출(콜백)할 때까지 B 작업을 동시에 처리)
패키지매니저
Nodejs의 기본 패키지매니저인 npm을 통해 프로세스 관리 패키지인 pm2를 설치하고 활용해 본다.
pm2 설치
D:\study\Nodejs>npm install pm2 -g
pm2 실행
D:\study\Nodejs>pm2 start main.js
-------------
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=C:\Users\LeeDahye\.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting D:\study\Nodejs\main.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 12360 │ 0s │ 0 │ online │ 0% │ 32.9mb │ Nameeeee │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
(멋진 로고를 출력한다..)
실행중인 프로그램 목록 조회
D:\study\Nodejs>pm2 list
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 12360 │ 3m │ 0 │ online │ 0% │ 30.7mb │ Nameeeee │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
실행 프로그램 중단
D:\study\Nodejs>pm2 stop main
[PM2] Applying action stopProcessId on app [main](ids: [ 0 ])
[PM2] [main](0) ✓
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 0 │ 0 │ 0 │ stopped │ 0% │ 0b │ Nameeeee │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
(stop main.js가 아니라 main이다. 파일명이 아니라 실행 프로그램 이름을 사용해야 함)
실행로그 확인
D:\study\Nodejs>pm2 log
pm2를 사용하여 main을 실행시켜 놓으면( —watch 조건 필요(pm2 start main.js —watch
)) 파일의 변경을 자동으로 감지하여 새로고침만으로 반영해 준다. 일일이 껐다켰다할 필요가 없어서 아주 편해짐.
'공부 저장소 > Node.js' 카테고리의 다른 글
[생활코딩 Node.js] 13. 입력 페이지 만들기 (0) | 2022.08.04 |
---|---|
[생활코딩 Node.js] 12. HTML form태그 (0) | 2022.08.04 |
[생활코딩 Node.js] 10. 함수를 이용한 코드 정리 (0) | 2022.07.31 |
[생활코딩 Node.js] 9. 파일을 이용한 list 생성 (0) | 2022.07.31 |
[생활코딩 Node.js] 8. 홈 페이지 만들기 (0) | 2022.07.31 |