nodejs製作成windows服務

使用了node-windows 模塊 此模塊其實底層也是採用winsm.exe

本來正常不需要start.js從cmd啓動服務的,但是不知道怎麼按照官網文檔使用不起來,所以曲線救國,調用cmd來啓動了
官網地址

//全局安裝模塊
npm install node-windows -g
//然後再目標工程根路徑
npm link node-windows

//這步不知道是否多餘 安裝node調用cmd窗口的模塊
npm install node-cmd

在根路徑創建start.js腳本

var cmd=require('node-cmd');
 cmd.run('npm run start');

在根路徑創建install.js腳本

var Service = require('node-windows').Service;
 
// Create a new service object
var svc = new Service({
  name:'regexV1',
  description: 'the regexper help.',
  script: 'C:/develop/regexper-static-master/start.js'
});
 
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});
 
svc.install();

創建uninstall.js腳本

var Service = require('node-windows').Service;
var svc = new Service({
  name:'regexV1',
  description: 'the regexper help.',
  script: 'C:/develop/regexper-static-master/start-app.js'
});
 
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});
 
svc.uninstall();

在根路徑打開cmd窗口 執行

node install.js 

會在根目錄生成daemon文件夾 裏面有日誌和配置 和一個exe執行文件

此時去服務管理列表裏就能看到創建出來的服務了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章