简体   繁体   English

Nodejs - 如何跟踪错误?

[英]Nodejs - how to track after errors?

Yesterday I deployed my first nodejs app everything was fine, I woke up today and I saw the server stoped because error with some package.昨天我部署了我的第一个 nodejs 应用程序,一切都很好,我今天醒来发现服务器因为某些包错误而停止。 How can I track after all the errors, and make the server not to go down even there is some error?如何在所有错误之后进行跟踪,并使服务器即使出现错误也不会停机?

You can use PM2 as a process manager您可以使用PM2作为进程管理器

npm install pm2 -g

Good practice is generate a ecosystem config好的做法是生成 生态系统配置

pm2 ecosystem

ecosystem.config.ts生态系统配置文件

    apps: [
        {
            name: 'My application',
            script: 'npm',
            args: 'start',
            autorestart: true,
            instance_var: 'my-app',
            out_file: '/path/to/out.log',
            log_file: '/path/to/global.log',
            error_file: '/path/to/error.log'
        }
    ]
};

Also you can use npx instead of globally install pm2.您也可以使用npx而不是全局安装 pm2。 You can start you app with pm2 start ecosystem.config.js (or npx pm2 start ecosystem.config.js if you like).您可以使用pm2 start ecosystem.config.js (或npx pm2 start ecosystem.config.js如果您愿意)启动您的应用程序。

To see list of started processes and its statuses use pm2 list or npx pm2 list要查看已启动进程列表及其状态,请使用pm2 listnpx pm2 list

You can use nodemon to restart the node-server after crashing - see here :您可以在崩溃后使用 nodemon 重新启动节点服务器 - 请参见此处

install:安装:

npm install -g nodemon

run: nodemon -x 'node app.js ||运行: nodemon -x 'node app.js || touch app.js'触摸 app.js'


alternative this should also work:替代方法这也应该有效:

npm install pm2 -g
pm2 start server.js --watch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM