node 在window下永远不挂

test.js


var Service = require('node-windows').Service;

// Create a new service object

var svc = new Service({ name:'node开机自启', script: require('path').join(__dirname,' check.js')});  // check .js 为  node check

svc.on('install',function(){

svc.start();

});

svc.install();


链接地址 :https://github.com/coreybutler/node-windows



创建监听端口 服务挂掉自动重启

check.js

var exec = require('child_process').exec; check(); function check() { var last = exec('lsof -i:8888'); last.on('exit', function (code) { if (code != 0) { run(); console.log('主服务已关闭,马上重启'); } else { console.log('主服务活跃中') } }); setTimeout(check, 1000);} function run() { var last = exec('node index.js'); last.on('exit', function (code) { if (code == 0) { console.log('主服务重启成功'); } else { console.log('主服务重启失败'); } })}

8888端口 为监听的端口



node代码

index.js

var http = require('http');           

http.createServer(function(request,response){ 

       response.writeHead(200,{'Content-Type':'text/plain'}) 

       response.end('Hello world\n');}).listen(8888);

       console.log('Server runnint at http:localhost:8888');

你可能感兴趣的:(node 在window下永远不挂)