繁体   English   中英

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

[英]node.js http server as a Windows service

我在 Node.js 中创建了一个简单的 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"

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

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

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

窗口服务

将 Node.JS 程序作为本机 Windows 服务运行。

npm 安装 windows 服务

使用这个,非常简单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'
});

// Listen 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')
});

// 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();

猜测一下,我会说该服务不知道在哪里可以找到节点二进制文件。 您可能已经更新了个人资料的 PATH 变量。 我的建议是始终对服务脚本中的完整路径进行硬编码。

正如其他问题中提到的,我想在这里分享(因为它还没有被引用)一个名为WinSer的 node.js 模块,它包装了 NSSM 并且它的用法非常简单,也许有一天它可以帮助某人。

:)

你可以试试包qckwinsvc 首先全局安装:

npm install -g qckwinsvc

然后从cmd:

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

卸载:

qckwinsvc --uninstall

查看下载次数总是一个好主意。

PM2好像赢了,很容易。

https://medium.com/@harshamw/deploying-a-node-js-application-in-iis-using-a-reverse-proxy-process-management-using-pm2-3d59b83d7f76

然后,您需要使用https://www.npmjs.com/package/pm2-windows-service在重新启动时将其作为 Windows 服务启动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM