如何将 node.js 安装为 Windows 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10547974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:41:09  来源:igfitidea点击:

How to install node.js as windows service?

node.jswindows-services

提问by TN.

I have downloaded node.jsexecutable. How can I run that executable as windows service? I cannot use standard node.js installer, since I need to run multiple version of node.js concurrently.

我已经下载了node.js可执行文件。如何将该可执行文件作为 Windows 服务运行?我不能使用标准的 node.js 安装程序,因为我需要同时运行多个版本的 node.js。

回答by Corey

Late to the party, but node-windowswill do the trick too.

聚会迟到了,但节点窗口也能做到这一点。

enter image description here

在此处输入图片说明

It also has system logging built in.

它还内置了系统日志记录。

enter image description here

在此处输入图片说明

There is an API to create scripts from code, i.e.

有一个 API 可以从代码创建脚本,即

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\path\to\helloworld.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

FD: I'm the author of this module.

FD:我是这个模块的作者。

回答by Hariram Nandagopal

I found the thing so useful that I built an even easier to use wrapper around it (npm, github).

我发现它非常有用,以至于我围绕它构建了一个更易于使用的包装器(npmgithub)。

Installing it:

安装它:

npm install -g qckwinsvc

Installing your service:

安装您的服务:

qckwinsvc

qckwinsvc

prompt: Service name: [name for your service]
prompt: Service description: [description for it]
prompt: Node script path: [path of your node script]
Service installed

Uninstalling your service:

卸载您的服务:

qckwinsvc --uninstall

qckwinsvc --卸载

prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

回答by Predrag Stojadinovi?

WinSeris a node.js friendly wrapper around the popular NSSM (Non-Sucking Service Manager)

WinSer是一个对流行的NSSM(Non-Sucking Service Manager)友好的 node.js 包装

回答by Michael Horojanski

From this blog

从这个博客

Next up, I wanted to host node as a service, just like IIS. This way it'd start up with my machine, run in the background, restart automatically if it crashes and so forth.

This is where nssm, the non-sucking service manager, enters the picture. This tool lets you host a normal .exe as a Windows service.

Here are the commands I used to setup an instance of the your node application as a service, open your cmd like administrator and type following commands:

nssm.exe install service_name c:\your_nodejs_directory\node.exe c:\your_application_directory\server.js
net start service_name

接下来,我想将节点作为服务托管,就像 IIS 一样。这样它会在我的机器上启动,在后台运行,如果它崩溃会自动重启等等。

这就是nssm,非吮吸服务管理器,进入图片的地方。此工具可让您将普通 .exe 作为 Windows 服务托管。

以下是我用来将您的节点应用程序的实例设置为服务的命令,像管理员一样打开您的 cmd 并键入以下命令:

nssm.exe install service_name c:\your_nodejs_directory\node.exe c:\your_application_directory\server.js
net start service_name

回答by KFL

I'm not addressing the question directly, but providing an alternative that might also meet your requirement in a more node.js fashion way.

我不是直接解决这个问题,而是提供一种替代方案,它也可能以更 node.js 的方式满足您的要求。

Functionally the requirements are:

功能上的要求是:

  1. Have the logic (app) running in the background
  2. Be able to start/stop the logic
  3. Automatically start the logic when system boots up
  1. 让逻辑(应用程序)在后台运行
  2. 能够启动/停止逻辑
  3. 系统启动时自动启动逻辑

These requirements can be satisfied by using a process manager (PM) and making the process manager start on system startup. Two good PMs that are Windows-friendly are:

这些要求可以通过使用进程管理器 (PM) 并在系统启动时启动进程管理器来满足。两个对 Windows 友好的优秀 PM 是:

To make the PM start automatically, the most simple way is to create a scheduled task with a "At Startup" trigger:

要让 PM 自动启动,最简单的方法是创建一个带有“At Startup”触发器的计划任务:

enter image description here

在此处输入图片说明

回答by KFL

The process manager + task scheduler approach I posted a year ago works well with some one-off service installations. But recently I started to design system in a micro-service fashion, with many small services talking to each other via IPC. So manually configuring each service has become unbearable.

我一年前发布的流程管理器 + 任务调度程序方法适用于一些一次性服务安装。但最近我开始以微服务的方式设计系统,许多小服务通过 IPC 相互通信。于是手动配置每个服务就变得难以忍受了。

Towards the goal of installing services without manual configuration, I created serman, a command line tool (install with npm i -g serman) to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run

为了实现无需手动配置即可安装服务的目标,我创建了serman,这是一个命令行工具(使用 安装npm i -g serman)来将可执行文件安装为服务。您需要编写的所有内容(并且只编写一次)是一个简单的服务配置文件以及您的可执行文件。跑

serman install <path_to_config_file>

will install the service. stdoutand stderrare all logged. For more info, take a look at the project website.

将安装服务。stdout并且stderr都被记录了。有关更多信息,请查看项目网站

A working configuration file is very simple, as demonstrated below. But it also has many useful features such as <env>and <persistent_env>below.

工作配置文件非常简单,如下所示。但它也有许多有用的功能,例如<env><persistent_env>以下。

<service>
  <id>hello</id>
  <name>hello</name>
  <description>This service runs the hello application</description>

  <executable>node.exe</executable>

  <!-- 
       {{dir}} will be expanded to the containing directory of your 
       config file, which is normally where your executable locates 
   -->
  <arguments>"{{dir}}\hello.js"</arguments>

  <logmode>rotate</logmode>

  <!-- OPTIONAL FEATURE:
       NODE_ENV=production will be an environment variable 
       available to your application, but not visible outside 
       of your application
   -->
  <env name="NODE_ENV" value="production"/>

  <!-- OPTIONAL FEATURE:
       FOO_SERVICE_PORT=8989 will be persisted as an environment
       variable machine-wide.
   -->
  <persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>