Auto start node.js server on boot

后端 未结 10 1360
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 17:16

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

10条回答
  •  感动是毒
    2020-11-30 17:48

    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.

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

    p.s.

    I found the thing so useful that I built an even easier to use wrapper around it (npm, github).

    Installing it:

    npm install -g qckwinsvc
    

    Installing your service:

    > 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
    

    Uninstalling your service:

    > qckwinsvc --uninstall
    prompt: Service name: [name of your service]
    prompt: Node script path: [path of your node script]
    Service stopped
    Service uninstalled
    

提交回复
热议问题