这不是要在node中配置的内容.总之,这纯粹是操作系统的责任(在你的例子中是Windows).实现这一点最可靠的方法是通过Windows服务.
这个super easy模块将 node 脚本安装为windows服务,它被称为node-windows(npmgithubdocumentation).我以前用过,工作起来很有魅力.
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();
p.s.
我发现这个东西非常有用,所以我在它周围构建了一个更容易使用的包装(npmgithub).
安装它:
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