一般部署nodejs的項(xiàng)目憨琳,大家都會(huì)用到forever這個(gè)庫(kù)锰蓬,這個(gè)庫(kù)相當(dāng)好用,可以讓nodejs的站點(diǎn)在后臺(tái)跑赋咽,不需要cmd的窗口一直開著旧噪。在windows下,如果用戶一直不注銷脓匿,這種方式是可行的淘钟,但在服務(wù)器上的話就麻煩了,因?yàn)榉?wù)器在部署完成后陪毡,一般都會(huì)注銷米母,那么站點(diǎn)就掛了。
因此需要把它部署成windows服務(wù)毡琉,廢話不多說铁瞒,部署成windows服務(wù)需要幾個(gè)步驟。
1. 全局安裝node-windows的庫(kù)
npm i -g node-windows
2. 在項(xiàng)目中新建一個(gè)安裝文件nw.js
let Service = require('node-windows').Service;
let svc = new Service({
name: 'ele4React', //服務(wù)名稱
description: 'ele4React', //描述
script: 'E:/mykoa/index.js' //nodejs項(xiàng)目要啟動(dòng)的文件路徑
});
svc.on('install', () => {
svc.start();
});
svc.install();
3. 在項(xiàng)目中新建一個(gè)卸載文件nw-uninstall.js
let Service = require('node-windows').Service;
let svc = new Service({
name: 'ele4React', //服務(wù)名稱
description: 'ele4React', //描述
script: 'E:\mykoa\index.js' //nodejs項(xiàng)目要啟動(dòng)的文件路徑
});
svc.on('uninstall',function(){
console.log('Uninstall complete.');
console.log('The service exists: ',svc.exists);
});
svc.uninstall();
4. 執(zhí)行命令
node nw.js //安裝服務(wù)
node nw-uninstall //卸載服務(wù)
服務(wù)安裝完成
image.png