在Debian上监控和调试Node.js应用有多种方法,以下是一些常见的方法:
使用pm2进行进程管理
sudo npm install pm2 -g
pm2 start app.js --name my-app
pm2 monit
pm2 logs my-app
使用nodemon进行开发环境监控
sudo npm install nodemon -g
nodemon app.js
使用Prometheus和Grafana进行高级监控
sudo apt-get update
sudo apt-get install prometheus
/etc/prometheus/prometheus.yml 文件,添加你的Node.js应用的监控配置。sudo systemctl start prometheus
sudo apt-get install grafana
sudo systemctl start grafana-server
使用New Relic或Datadog进行商业监控
使用systemd进行系统级监控
sudo nano /etc/systemd/system/my-app.service
[Unit]
Description=My Node.js Application
After=network.target
[Service]
ExecStart=/usr/bin/node /path/to/your/app.js
Restart=always
User=your-user
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start my-app
sudo systemctl enable my-app
sudo systemctl status my-app
使用Node.js内置模块进行监控
process.hrtime() 记录请求处理时间,使用 process.memoryUsage() 监控内存使用情况。使用debug包
npm install debug
const debug = require('debug')('myapp');
debug('Hello, debug!');
DEBUG=myapp node app.js
使用Visual Studio Code进行调试
使用Node.js内置调试器
node inspect YOUR_FILE_PATH.js 启动调试模式,使用Chrome DevTools进行调试。使用第三方调试工具
npm install -g node-inspector
node-inspector --web-port 8888
node --inspect your_script.js
ndb your_script.js
通过以上方法,你可以在Debian上为你的Node.js应用添加不同级别的监控和调试功能,根据你的需求选择合适的方法。