2

I’m developing a Node.js application which in a user signs up in three levels. the problem is when a user encounters and error in any of the levels, the whole server reset and the rest of the user sessions get close. the reason that server restarts is that I’m using forever start app to start my app,otherwise it goes down completely.

  1. how to stop the server to stops completely when just one users encounters any error?

  2. how to start each users in an individual thread(by thread i mean an isolate environment)?

  • Welcome to Stack Overflow! You might want to check out [how to ask a question](http://stackoverflow.com/help/how-to-ask). Formatting your question correctly will go a long way to getting you the answer you are looking for. – Gary Storey Jun 01 '15 at 16:15
  • The real issue is that you're not handling error gracefully. I would recommend starting with the top 3 articles of this search: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=nodejs%20graceful%20error%20handling – Mike Perrenoud Jun 01 '15 at 16:16
  • You're definitely not handling an error correctly and your server is crashing, only to be restarted by forever. We need to see your code. You shouldn't need to use multi-threading to solve this. – CatDadCode Jun 01 '15 at 16:18
  • @MichaelPerrenoud regardless of handling of any errors, the server shouldn't stop.only the user session which encounters the error.for example in `asp.net` when trying access to a `null` object it throws `object reference not set to an instance of an object` but `iis` doesn't stop – Saeed Oraji Jun 01 '15 at 16:24
  • @AlexFord thread was an example I mean an isolate environment – Saeed Oraji Jun 01 '15 at 16:26
  • @SaeedOraji unfortunately that's not how Node works. An unhandled error can cause the server to restart. – Mike Perrenoud Jun 01 '15 at 16:40
  • @SaeedOraji understanding how Node actually works is going to paramount to how you approach problems. I'd say you're going to want to spend some time reading through articles or videos that really detail that for you. – Mike Perrenoud Jun 01 '15 at 16:42
  • @SaeedOraji here is a pretty good article: http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js – Mike Perrenoud Jun 01 '15 at 16:44
  • @MichaelPerrenoud thanks for the article and your comment – Saeed Oraji Jun 01 '15 at 16:59

1 Answers1

2

Considering @Michael 's answer

unfortunately that's not how Node works. An unhandled error can cause the server to restart.

The only way is to handle execptions in a proper way. So you can check this answers comments about how to handle an uncaughtException

var handleRequest = function(req, res) {
    res.writeHead(200);
    res1.end('Hello, World!\n');
};
var server = require('http').createServer(handleRequest);
process.on('uncaughtException', function(ex) {
    // do something with exception
});
server.listen(8080);
console.log('Server started on port 8080');
Community
  • 1
  • 1
Ghasem
  • 14,455
  • 21
  • 138
  • 171