I am writing my first NodeJS app but for some reason it seems to die unexpectedly after a short amount of time. I have no clue what would be causing it. The process runs fine, even works as expected, then for some reason it just stops. The nohup log does not show an error or any feedback.
I have tried running this in debug mode but its the same, no information. The trace is no help.
I run the process via nohup:
nohup node app.js &
Code:
var http = require('http');
var server = http.createServer().listen(8000);
var io = require('socket.io').listen(server);
var cookie_reader = require('cookie');
var querystring = require('querystring');
// Store the session cookie set by Django
io.configure(function(){
io.set('authorization', function(data, accept){
if(data.headers.cookie){
data.cookie = cookie_reader.parse(data.headers.cookie);
return accept(null, true);
}
return accept('error', false);
});
io.set('log level', 1);
});
io.sockets.on('connection', function (socket) {
socket.on('shoutbox_send', function(message){
values = querystring.stringify({
comment: message,
sessionid: socket.handshake.cookie['sessionid'],
});
try {
var options = {
host: 'www.example.com',
port: 80,
path: '/shoutbox/node_api',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': values.length
}
};
var req = http.get(options, function(res){
res.setEncoding('utf8');
res.on('data', function(message){
socket.emit('shoutbox_receive', message);
socket.broadcast.emit('shoutbox_receive', message);
});
});
req.write(values);
req.end();
} catch (err) {
console.log(err);
socket.emit('shoutbox_receive', err);
}
});
});
Thanks in advance,
I faced a similar problem ( but don't know whether there is a common reason )
Solution :
nohup node app.js &
as normalLast point is important I was not exiting from the ssh correctly hence it was getting killed.
If you run your app with Forever.js, it will have 2 effects that might be helpful for you:
(1) it will automatically restart your app when it terminates
(2) it should give you an exit message like
warn: Forever detected script exited with code: [EXIT CODE]
which should make it easier to track the issue down
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With