本文介绍了引导时自动启动node.js服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何node.js专家都可以告诉我如何在机器启动时配置节点JS以自动启动服务器吗? 我在Windows上

Can any node.js experts tell me how I might configure node JS to autostart a server when my machine boots? I'm on Windows

推荐答案

这根本不是要在node.js中配置的东西,这完全是操作系统的职责(在您的情况下为Windows).实现此目标的最可靠方法是通过Windows服务.

This isn't something to configure in node.js at all, this is purely OS responsibility (Windows in your case). The most reliable way to achieve this is through a Windows Service.

有一个 super easy 模块可将节点脚本安装为Windows服务,称为 node-windows (, github ,).我以前用过,像魅惑一样工作.

There's this super easy module that installs a node script as a windows service, it's called node-windows (npm, github, documentation). I've used before and worked like a charm.

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


ps.s.

我发现这东西非常有用,以至于我围绕它构建了一个更易于使用的包装器( npm github ).

I found the thing so useful that I built an even easier to use wrapper around it (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 --uninstall
prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

这篇关于引导时自动启动node.js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:36