How do I run a node.js app as a background service?

前端 未结 26 976
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 05:28

Since this post has gotten a lot of attention over the years, I\'ve listed the top solutions per platform at the bottom of this post.


Original post

相关标签:
26条回答
  • 2020-11-22 06:25

    Node.js as a background service in WINDOWS XP

    • Kudos goes to Hacksparrow at: http://www.hacksparrow.com/install-node-js-and-npm-on-windows.html for tutorial installing Node.js + npm for windows.
    • Kudos goes to Tatham Oddie at: http://blog.tatham.oddie.com.au/2011/03/16/node-js-on-windows/ for nnsm.exe implementation.

    Installation:

    1. Install WGET http://gnuwin32.sourceforge.net/packages/wget.htm via installer executable
    2. Install GIT http://code.google.com/p/msysgit/downloads/list via installer executable
    3. Install NSSM http://nssm.cc/download/?page=download via copying nnsm.exe into %windir%/system32 folder
    4. Create c:\node\helloworld.js

      // http://howtonode.org/hello-node
      var http = require('http');
      var server = http.createServer(function (request, response) {
          response.writeHead(200, {"Content-Type": "text/plain"});
          response.end("Hello World\n");
      });
      server.listen(8000);
      console.log("Server running at http://127.0.0.1:8000/");
      
    5. Open command console and type the following (setx only if Resource Kit is installed)

      C:\node> set path=%PATH%;%CD%
      C:\node> setx path "%PATH%"
      C:\node> set NODE_PATH="C:\Program Files\nodejs\node_modules"
      C:\node> git config --system http.sslcainfo /bin/curl-ca-bundle.crt    
      C:\node> git clone --recursive git://github.com/isaacs/npm.git    
      C:\node> cd npm    
      C:\node\npm> node cli.js install npm -gf   
      C:\node> cd ..    
      C:\node> nssm.exe install node-helloworld "C:\Program Files\nodejs\node.exe" c:\node\helloworld.js    
      C:\node> net start node-helloworld
      
    6. A nifty batch goodie is to create c:\node\ServiceMe.cmd

      @echo off
      nssm.exe install node-%~n1 "C:\Program Files\nodejs\node.exe" %~s1
      net start node-%~n1
      pause
      

    Service Management:

    • The services themselves are now accessible via Start-> Run-> services.msc or via Start->Run-> MSCONFIG-> Services (and check 'Hide All Microsoft Services').
    • The script will prefix every node made via the batch script with 'node-'.
    • Likewise they can be found in the registry: "HKLM\SYSTEM\CurrentControlSet\Services\node-xxxx"
    0 讨论(0)
  • 2020-11-22 06:25

    use nssm the best solution for windows, just download nssm, open cmd to nssm directory and type

    nssm install <service name> <node path> <app.js path> 
    
    eg: nssm install myservice "C:\Program Files\nodejs" "C:\myapp\app.js" 
    

    this will install a new windows service which will be listed at services.msc from there you can start or stop the service, this service will auto start and you can configure to restart if it fails.

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