Friday 15 March 2013

node.js - using spawn to restart NODE server if error happened -


I'm trying this code to restart the node server if it has stopped accidentally, in this example "X" is not defined, so an error should be restarted, but in fact the server exits completely.

  var http = need ('http'); Var spawn = required ('child_process'). Eggs; Var Server = http.createServer (function (request, response) {console.log (x);}); Server.listen (8125, 'localhost', function (error) {if (err) spawn ('node', ['. Server.js'])}}; Console.log (' server http://127.0.0.1 : 8125 / ');    

The problem with your code is that you The server can not find an error. Listen, you must add an event handler to your main process, so when an exception or error occurs, the event handler will handle this exception, the code below will handle an error in the node.js script One Example: var http = Required ('http'); Var Server = http.createServer (function (request, response) {console.log (x);}); Server Add some code ('I am here');});); Console to act upon action ('8125,' localhost '); Process.on (' uncaughtException ', function (mistake) {// excitation console.log. Log (running on 'server http://127.0.0.1:8125/');


No comments:

Post a Comment