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');

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容