With a simple http server in node.js, how to find out why it's crashed? -
i running basic http server serve static files in node.js express
:
var setting = require('../setting') var express = require('express'), app = express(), port = setting.http.port; app.use(express.static(__dirname + '/' + setting.http.static_dir)); app.listen(port);
i know can use forever
restart server if it's dead reason. but, there way log when server crashed can know reason why it's crashed , when it's crashed?
with forever
:
forever -o out.log -e err.log my-script.js
if want log express server errors, use error handler generated express cli tool:
server.on('error', onerror); /** * event listener http server "error" event. */ function onerror(error) { if (error.syscall !== 'listen') { throw error; } var bind = typeof port === 'string' ? 'pipe ' + port : 'port ' + port; // handle specific listen errors friendly messages switch (error.code) { case 'eacces': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'eaddrinuse': console.error(bind + ' in use'); process.exit(1); break; default: throw error; } }
where server
instance created running http.createserver(app)
.
if want log callback errors, have this:
brokenfunction(function(err) { if(err) return errorhandler(err); //... }); function errorhandler(err) { //do error printing console or writing file. console.log(err); }
where brokenfunction
function returns error callback , errorhandler
function performs actions want occur on error.
Comments
Post a Comment