当前位置: 首页 > news >正文

在windows服务器上使用node-windows部署nodeJS服务

一般部署nodejs的项目,大家都会用到forever这个库,这个库相当好用,可以让nodejs的站点在后台跑,不需要cmd的窗口一直开着。在windows下,如果用户一直不注销,这种方式是可行的,但在服务器上的话就麻烦了,因为服务器在部署完成后,一般都会注销,那么站点就挂了。

因此需要把它部署成windows服务,废话不多说,部署成windows服务需要几个步骤。

1. 全局安装node-windows的库

npm i -g node-windows

2. 在项目中新建一个安装文件nw.js 

let path = require('path');let Service = require('node-windows').Service;// Create a new service object
let svc = new Service({name:'node windows server test', //名称description: 'The socket.io nodejs server test ',//描述script:  path.resolve('./index.js'),//node执行入口nodeOptions: ['--harmony','--max_old_space_size=4096']
});// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){svc.start();
});svc.install();

3.在项目中新建一个卸载文件nw-uninstall.js

// 卸载文件nw-uninstall.jslet Service = require('node-windows').Service;let svc = new Service({name:'node windows server test', //名称description: 'The socket.io nodejs server test ',//描述script:  path.resolve('./index.js'),//node执行入口nodeOptions: ['--harmony','--max_old_space_size=4096']});svc.on('uninstall',function(){console.log('Uninstall complete.');console.log('The service exists: ',svc.exists);});svc.uninstall();

4.执行命令 

node nw.js //安装服务       
node nw-uninstall //卸载服务

注意:每次修改nw.js文件后,需要重新执行node nw.js

查看服务,已经在运行中了

相关文章:

  • Windows服务与网络端口
  • 用 vs 2017创建 windows 服务
  • 如何将nginx安装到Windows服务
  • 【转】Go语言和Windows服务
  • C++语言编写windows服务
  • Golang 实现Windows服务
  • Qt实现Windows服务程序
  • minio:安装部署并安装成windows服务
  • C# 创建Windows Service(Windows服务)程序
  • 如何创建Windows服务
  • window服务是什么?
  • Windows服务(Service)安装及启动停止方案
  • Windows 服务介绍(本地系统、网络服务、本地服务以及相关的power shell命令
  • windows服务ServiceProcess
  • Windows 服务
  • js 解析json数据实现快递包裹的查询
  • 12、 一键查快递
  • 已知顺丰快递既可以发陆运,也可以发空运;EMS只能发空运,圆通只能发陆运。 小明现在发送快递,为其设计两个方法,分别用来发空运和陆运。
  • valgrind 工具使用
  • valgrind 工具介绍和简单的使用