[英]How to install node.js as windows service?
聚会迟到了,但节点窗口也能做到这一点。
它还内置了系统日志记录。
有一个 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:我是这个模块的作者。
我发现它非常有用,以至于我围绕它构建了一个更易于使用的包装器( npm 、 github )。
安装它:
npm install -g 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
卸载您的服务:
qckwinsvc --卸载
prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled
WinSer是一个对流行的NSSM(Non-Sucking Service Manager)友好的 node.js 包装器
接下来,我想将节点作为服务托管,就像 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
https://nssm.cc/服务助手适用于通过我从 nssm 使用的批处理文件创建 Windows 服务,并且适用于任何应用程序和任何文件
我一年前发布的流程管理器 + 任务调度程序方法适用于一些一次性服务安装。 但最近我开始以微服务的方式设计系统,许多小服务通过 IPC 相互通信。 于是手动配置每个服务就变得难以忍受了。
为了实现无需手动配置即可安装服务的目标,我创建了serman ,这是一个命令行工具(使用npm i -g serman
安装)以将可执行文件安装为服务。 您需要编写的所有内容(并且只编写一次)是一个简单的服务配置文件以及您的可执行文件。 跑步
serman install <path_to_config_file>
将安装服务。 stdout
和stderr
都被记录了下来。 有关更多信息,请查看项目网站。
工作配置文件非常简单,如下所示。 但它也有许多有用的特性,例如下面的<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.