node.js http server as a Windows service

后端 未结 6 764
再見小時候
再見小時候 2021-02-01 21:42

I created a simple http server in Node.js.

I wanted to make it run permanently on my Windows 2008 machine, so that, if the computer reboots, it automatically restarts.

相关标签:
6条回答
  • 2021-02-01 22:08

    You could try the package qckwinsvc. First install it globally:

    npm install -g qckwinsvc

    And then from the cmd:

    qckwinsvc
    prompt: Service name: [...]
    prompt: Service description: [...]
    prompt: Node script path: [/path/to/.js file]
    

    To uninstall:

    qckwinsvc --uninstall

    0 讨论(0)
  • 2021-02-01 22:12

    In the past, I've used NSSM for running Node.js applications as services on Windows. It works quite well, and can be configured to automatically restart your application in the event of a crash.

    http://nssm.cc/usage

    nssm install YourService "C:\Program Files\Node.js\node.exe" "C:\something\something.js"
    
    0 讨论(0)
  • 2021-02-01 22:15

    At a guess, I'd say that the service doesn't know where to find the node binary. You've probably updated your profile's PATH variable. My recommendation is to ALWAYS hard code the full path in service scripts.

    0 讨论(0)
  • 2021-02-01 22:21

    As I recall, the Service runtime environment isn't the same as running something under the command shell. In particular, Services are required to respond to messages from the system to indicate their running status, as you've seen :-)

    This must be a solved problem, though...

    Sure enough: https://npmjs.org/package/windows-service

    windows-service

    Run Node.JS programs as native Windows Services.

    npm install windows-service

    0 讨论(0)
  • 2021-02-01 22:26

    As mentioned in others questions about it, I'd like to share here (because it wasn't referred yet) a node.js module called WinSer that wraps NSSM and its usage is very simple, maybe it helps someone someday.

    : )

    0 讨论(0)
  • 2021-02-01 22:27

    Use this one, really simple https://github.com/coreybutler/node-windows

    Create two js file on your project. And run those as

    node your_service.js node your_service_remove.js

    For install:

     /**
     * Created by sabbir on 08/18/2015.
     */
    //ref: https://github.com/coreybutler/node-windows
    var Service = require('node-windows').Service;
    
    // Create a new service object
    var svc = new Service({
      name:'nodeDemoApp',
      description: 'The nodejs.org example web server.',
      script: 'D:\\NodeJS\\demoWeb\\bin\\www'
    });
    
    // Listen for the "install" event, which indicates the
    // process is available as a service.
    svc.on('install',function(){
      svc.start();
    });
    
    svc.install();
    

    For uninstall:

    var Service = require('node-windows').Service;
    
    // Create a new service object
    var svc = new Service({
      name:'nodeDemoApp',
      script: require('path').join(__dirname,'bin\\www')
    });
    
    // Listen for the "uninstall" event so we know when it's done.
    svc.on('uninstall',function(){
      console.log('Uninstall complete.');
      console.log('The service exists: ',svc.exists);
    });
    
    // Uninstall the service.
    svc.uninstall();
    
    0 讨论(0)
提交回复
热议问题