简体   繁体   English

Node.Js 服务器不断崩溃,没有错误

[英]Node.Js Server keeps crashing with no error

I am currently taking care of a project for a business that is basically a real time message board used to interact with other employees.我目前正在为一家企业负责一个项目,该项目基本上是一个用于与其他员工互动的实时留言板。

I have taken this over after the basics had already been developed and I am quite new to Node, and developing in general.在已经开发了基础知识之后,我接手了这个工作,而且我对 Node 还是很陌生,并且在进行总体开发。

Our server just quits unexpectedly at random times with no error.我们的服务器只是在随机时间意外退出,没有错误。 It happens up to once per day, sometimes with very little server load, sometimes with heavy server load.它每天最多发生一次,有时服务器负载很少,有时服务器负载很重。

There is a listener for uncaught exceptions:有一个未捕获异常的侦听器:

    process.on('uncaughtException', function(err) {
    // handle the error safely
    console.log("Uncaught exception: "+err);
    });

and we also print errors to console and a log file, but neither ever shows an error just before it quits!并且我们还将错误打印到控制台和日志文件,但在退出之前都没有显示错误!

We are using express and socket.io我们正在使用 express 和 socket.io

Any help would be appreciated.任何帮助,将不胜感激。

This is basically the code we have in the server.这基本上是我们在服务器中的代码。 I am logging all the different processes I can see and I am not getting any error messages at all and it always seems to crash when something different is executing:我正在记录我可以看到的所有不同进程,但我根本没有收到任何错误消息,并且在执行不同的操作时它似乎总是崩溃:

    process.on('uncaughtException', function(err) {
    // handle the error safely
    console.log("Uncaught exception: "+err);
    });

app.configure(function(){
        app.use(express.cookieParser());    // Allow parsing of cookies
        app.use(express.json());                            // Support JSON parsng
        app.use(express.urlencoded());                      // Support URL encoded params
        app.set('view engine', 'ejs');                      // Define view engine as EJS
        app.use(express.session({store: sessionStore, secret: sessSecret, key: sessKey}));
        app.use(app.router);                                // Use routes
        app.use("/public", express.static(__dirname + '/public'));     // Define public directory
    });



    var server = https.createServer(options, app).listen(app_port, function(){
        console.log("Express server listening on port " + app_port);
    });

    // Require our routes module
    var routesModule = require('./modules/routes.js')(app);
    var svc_io  = require("/usr/lib/node_modules/socket.io/index.js")(https);
    var dbmodule = require("./modules/db.js");


var moment = require('/usr/lib/node_modules/moment/moment.js');
    var fs = require('/usr/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js');
    var util = require('util');

    console.log = function(d){
        var time = moment().format('DD-MMM-YYYY HH:mm:ss.SSS');

        var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags: 'a'});
        var log_stdout = process.stdout;

        log_file.write(time + ' : ' + util.format(d) + '\n');
        log_stdout.write(time + ' : ' + util.format(d) + '\n');
        log_file.end();
    };

    var io = svc_io.listen(server);
    var socketModule = require('./modules/socket.js')(app, io, sessionStore, cookieParser, sessKey, sessSecret);

In case this helps anyone else reaching this question from Google: I had a crashing Node.js server with no errors at all, and was pulling my hair out for an hour before realising that it was because in my code somewhere I was writing to a file like writeFileSync("./foo.json", "…") , which would of course normally be fine, but this was causing the server to "crash" because I was using PM2 to "watch" the server directory for file changes - ie whenever a file changes in the server directory, PM2 restarts the server.万一这有助于其他任何人从谷歌解决这个问题:我有一个崩溃的 Node.js 服务器,根本没有错误,并且在意识到这是因为在我的代码中的某个地方我正在写一个小时之前我的头发像writeFileSync("./foo.json", "…") ,这当然通常没问题,但这导致服务器“崩溃”,因为我使用 PM2 来“监视”服务器目录以进行文件更改- 即,每当服务器目录中的文件发生更改时,PM2 都会重新启动服务器。 I solved this by adding a watch_ignore config for the .data folder and put foo.json in there.我通过为.data文件夹添加watch_ignore配置并将foo.json放在那里解决了这个问题。

I guess you need to comment this and yu will get the point of error.我想你需要对此发表评论,你会明白错误的地方。

// process.on('uncaughtException', function (err) {
// handle the error safely
//console.log("Uncaught exception: "+err);
// });

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

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