I want to be able throw a function when my node app returns error
Let's say I have a simple node app
var express= require('express');
var app = express();
var http = require('http');
app.get('/', function(req, res){
res.send('hello')
})
app.listen(2500);
if there is an error while running the app, it might be related to the port or i might not have a proper node package module installed i just want it to run a function.. how do i do that?
I want it to console.log('node crashed') for example
You can listen for uncaughtException event:
process.on('uncaughtException', err => console.log('Error:', err));
but you should really handle the errors where they appear, so e.g. you should handle the port binding error explicitly:
var server = app.listen(2500);
server.on('error', err => console.log('Listen error:', err.message));
and handle other errors in the same fashion. Otherwise you will not be able to do anything to actually recover from the error, other than saying: oops.