1安装包
输入以下命令,安装需要的包
?1 npm install node-windows -g
2编写自启动js
在目标server.js目录下新建auto_start_nodejs.js文件,将以下js代码拷贝至该文件
?1 let Service = require(‘node-windows‘).Service; ?2 let svc = new Service({ ?3 name: ‘node_test1‘, //服务名称 ??4 description: ‘测试项目服务器‘, //描述 ??5 script: ‘C:/www‘, //nodejs项目要启动的文件路径 ??6 ??wait:‘1‘, ?7 ??grow:‘0.25‘, ?8 ??maxRestarts:‘40‘ ?9 }); ?10 ?11 svc.on(‘install‘,()=>{ 12 svc.start(); 13 console.log(‘install complete.‘);}); 14 ?15 svc.on(‘uninstall‘,() =>{ 16 console.log(‘Uninstall complete.‘); 17 console.log(‘The service exists:‘,svc.exists); 18 }); 19 ?20 svc.on(‘alreadyinstalled‘,()=>{ 21 console.log(‘This service is already installed.‘); 22 }); 23 ?24 if(svc.exists) return svc.uninstall(); 25 svc.install(); 26 ?27 ?28 ?29
3运行服务
在cmd中,cd到auto_start_nodejs.js所在目录,运行一下命令
?1 node auto_start_nodejs.js
查看服务已启动
nodejs 配置服务自启动
原文地址:http://www.cnblogs.com/xiaopanlyu/p/7867590.html