node.js http服务器作为Windows服务

我在Node.js中创build了一个简单的http服务器。

我想让它在Windows 2008计算机上永久运行,这样,如果计算机重新启动,它会自动重新启动。

所以我用这个命令做了一个服务:

C:\Users\Administrator>sc create translate binPath= "node D:\Apps\translate\machine-learning-server\servertranslate.js" DisplayName= "Translation Server" 

然后开始:

 C:\Users\Administrator>sc start translate 

并得到以下错误信息:

 [SC] StartService FAILED 1053: The service did not respond to the start or control request in a timely fashion. 

程序工作正常,当我从命令行(而不是服务)启动它。

让计算机重新启动时自动重新启动node.js Web服务器的最简单方法是什么?

在过去,我使用NSSM在Windows上运行Node.js应用程序作为服务。 它工作得很好,可以配置为在发生崩溃时自动重启应用程序。

http://nssm.cc/usage

 nssm install YourService "C:\Program Files\Node.js\node.exe" "C:\something\something.js" 

我记得,Service运行时环境与在命令行下运行的东西不一样。 特别是,需要服务来响应来自系统的消息以指示其运行状态,如您所见:-)

这一定是一个解决的问题,虽然…

果然: https : //npmjs.org/package/windows-service

窗口服务

像本机Windows服务一样运行Node.JS程序。

npm安装windows-service

正如其他人在其他问题中提到的,我想在这里分享一个名为WinSer的node.js模块,它包装了NSSM,它的用法非常简单,也许它有一天会帮助别人。

:)

使用这个,真的很简单https://github.com/coreybutler/node-windows

在您的项目上创建两个js文件。 并运行这些

节点your_service.js节点your_service_remove.js

安装:

  /** * 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' }); // listn for the "install" event, which indicates the // process is available as a service. svc.on('install',function(){ svc.start(); }); svc.install(); 

对于卸载:

 var Service = require('node-windows').Service; // Create a new service object var svc = new Service({ name:'nodeDemoApp', script: require('path').join(__dirname,'bin\\www') }); // listn 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(); 

在猜测,我会说,该服务不知道在哪里可以找到节点的二进制文件。 你可能已经更新了你的配置文件的PATH变量。 我的建议是总是硬编码服务脚本的完整路径。

你可以试试这个包qckwinsvc 。 首先在全球安装:

npm install -g qckwinsvc

然后从cmd:

 qckwinsvc prompt: Service name: [...] prompt: Service description: [...] prompt: Node script path: [/path/to/.js file] 

要卸载:

qckwinsvc --uninstall