node.js http 服务器作为 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17640987/
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
node.js http server as a Windows service
提问by Erel Segal-Halevi
I created a simple http server in Node.js.
我在 Node.js 中创建了一个简单的 http 服务器。
I wanted to make it run permanently on my Windows 2008 machine, so that, if the computer reboots, it automatically restarts.
我想让它在我的 Windows 2008 机器上永久运行,这样,如果计算机重新启动,它会自动重新启动。
So I made it a service with this command:
所以我用这个命令把它变成了一个服务:
C:\Users\Administrator>sc create translate binPath= "node D:\Apps\translate\machine-learning-server\servertranslate.js" DisplayName= "Translation Server"
Then started it with:
然后开始:
C:\Users\Administrator>sc start translate
and got the following error message:
并收到以下错误消息:
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
The program works OK when I start it from the command line (not as a service).
当我从命令行(不是作为服务)启动它时,程序运行正常。
What is the easiest way to have a node.js web server that restarts automatically when the computer reboots?
让 node.js Web 服务器在计算机重新启动时自动重新启动的最简单方法是什么?
回答by Brad
In the past, I've used NSSM for running Node.js applications as services on Windows. It works quite well, and can be configured to automatically restart your application in the event of a crash.
过去,我使用 NSSM 在 Windows 上将 Node.js 应用程序作为服务运行。它工作得很好,并且可以配置为在发生崩溃时自动重启您的应用程序。
nssm install YourService "C:\Program Files\Node.js\node.exe" "C:\something\something.js"
回答by Mark Bessey
As I recall, the Service runtime environment isn't the same as running something under the command shell. In particular, Services are required to respond to messages from the system to indicate their running status, as you've seen :-)
我记得,服务运行时环境与在命令外壳下运行某些东西不同。特别是,服务需要响应来自系统的消息以指示其运行状态,如您所见:-)
This must be a solved problem, though...
这一定是一个解决的问题,虽然......
Sure enough: https://npmjs.org/package/windows-service
果然:https: //npmjs.org/package/windows-service
windows-service
Run Node.JS programs as native Windows Services.
npm install windows-service
窗口服务
将 Node.JS 程序作为本机 Windows 服务运行。
npm 安装 windows 服务
回答by Sabbir
Use this one, really simple https://github.com/coreybutler/node-windows
使用这个,非常简单 https://github.com/coreybutler/node-windows
Create two js file on your project. And run those as
在您的项目上创建两个 js 文件。并将它们作为
node your_service.js node your_service_remove.js
节点 your_service.js 节点 your_service_remove.js
For install:
安装:
/**
* Created by sabbir on 08/18/2015.
*/
//ref: https://github.com/coreybutler/node-windows
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'nodeDemoApp',
description: 'The nodejs.org example web server.',
script: 'D:\NodeJS\demoWeb\bin\www'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
For uninstall:
对于卸载:
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'nodeDemoApp',
script: require('path').join(__dirname,'bin\www')
});
// Listen for the "uninstall" event so we know when it's done.
svc.on('uninstall',function(){
console.log('Uninstall complete.');
console.log('The service exists: ',svc.exists);
});
// Uninstall the service.
svc.uninstall();
回答by Michael Schiebel
At a guess, I'd say that the service doesn't know where to find the node binary. You've probably updated your profile's PATH variable. My recommendation is to ALWAYS hard code the full path in service scripts.
猜测一下,我会说该服务不知道在哪里可以找到节点二进制文件。您可能已经更新了个人资料的 PATH 变量。我的建议是始终对服务脚本中的完整路径进行硬编码。
回答by FC.Araujo
As mentioned in others questions about it, I'd like to share here (because it wasn't referred yet) a node.js module called WinSerthat wraps NSSM and its usage is very simple, maybe it helps someone someday.
正如其他关于它的问题中提到的那样,我想在这里分享一个名为WinSer的 node.js 模块(因为它还没有被引用),它包装了 NSSM 并且它的用法非常简单,也许有一天它可以帮助某人。
: )
:)
回答by MrfksIV
You could try the package qckwinsvc. First install it globally:
你可以试试包qckwinsvc。首先全局安装:
npm install -g qckwinsvc
npm install -g qckwinsvc
And then from the cmd:
然后从cmd:
qckwinsvc
prompt: Service name: [...]
prompt: Service description: [...]
prompt: Node script path: [/path/to/.js file]
To uninstall:
卸载:
qckwinsvc --uninstall
qckwinsvc --uninstall

