Auto start node.js server on boot

后端 未结 10 1333
隐瞒了意图╮
隐瞒了意图╮ 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:42

    This can easily be done manually with the Windows Task Scheduler.

    • First, install forever.
    • Then, create a batch file that contains the following:

      cd C:\path\to\project\root
      call C:\Users\Username\AppData\Roaming\npm\forever.cmd start server.js
      exit 0
      
    • Lastly, create a scheduled task that runs when you log on. This task should call the batch file.

    0 讨论(0)
  • 2020-11-30 17:47

    Here is another solution I wrote in C# to auto startup native node server or pm2 server on Windows.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 17:49

    I would recommend installing your node.js app as a Windows service, and then set the service to run at startup. That should make it a bit easier to control the startup action by using the Windows Services snapin rather than having to add or remove batch files in the Startup folder.

    Another service-related question in Stackoverflow provided a couple of (apprently) really good options. Check out How to install node.js as a Windows Service. node-windows looks really promising to me. As an aside, I used similar tools for Java apps that needed to run as services. It made my life a whole lot easier. Hope this helps.

    0 讨论(0)
提交回复
热议问题