一、项目安装node-windows
   
    
     $ npm install node-windows@1.0.0-beta.6
    
   
    
     最新版本1.0.0-beta.7出现无法启动服务,这里用的版本是1.0.0-beta.6
    
   
    二、在项目根目录增加两个文件
   
nw.js和nw-uninstall.js
     
   
nw.js
let path = require('path');
 
let Service = require('node-windows').Service;
 
let svc = new Service({
  name:'api server', // 服务名称
  description: 'api server', // 服务描述
  script:  path.resolve('./bin/www'),// 项目入口文件
  nodeOptions: [
    '--harmony',
    '--max_old_space_size=4096'
  ]
});
 
svc.on('install',function(){
  svc.start();
});
 
svc.install();nw-uninstall.js
let path = require('path');
let Service = require('node-windows').Service;
let svc = new Service({
	name:'api server', // 服务名称
	description: 'api server', // 服务描述
	script:  path.resolve('./bin/www'),// 项目入口文件
	nodeOptions: [
	  '--harmony',
	  '--max_old_space_size=4096'
	]
  });
svc.on('uninstall',function(){
  console.log('Uninstall complete.');
  console.log('The service exists: ',svc.exists);
});
svc.uninstall();
    三、启动服务
   
    
     运行
    
    
     $ node nw
    
   
    
     完成添加到系统服务中
    
   
     
   
项目已可以访问
     
   
    四、卸载服务
   
    
     运行
    
    
     $ node nw-uninstall
    
   
     
   
已从服务中卸载
     
   
 
版权声明:本文为weixin_55510188原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
