node.js http server as a Windows service

后端 未结 6 840
再見小時候
再見小時候 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条回答
  •  Happy的楠姐
    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();
    

提交回复
热议问题