Node Windows Service for Running Node.js Application

node windows service

Why Run a Nodejs Application as a Service

The Windows Services is a core component of the Microsoft Windows Operating System. It enables the creation and management of long-running processes. Furthermore, unlike regular software which an end user launches and only runs when the user is online, Windows Services starts without any user intervention and continues running long after the user logs off. The services run in the background and usually kicks in when the machine boots.
However, the built-in service recovery for Windows services is fairly limited and cannot easily be configured from code. This is where node-windows service has its advantage.
The node-windows creates a wrapper around the Node.js script. This wrapper is responsible for restarting a failed service in an intelligent and configurable manner.

For example, if your script crashes due to an unknown error, node-windows will attempt to restart it. By default, this occurs every second. However, if the script has a fatal flaw that makes it crash repeatedly, it adds unnecessary overhead to the system. node-windows handles this by increasing the time interval between restarts and capping the maximum number of restarts.

Flexibility in the Configuration of Services

Press the Windows+R keys to open the Run dialog, type services.msc, press Enter, and the Services window will open.

  • View the list of installed services along with Service Name, descriptions and configuration.
  • Start, Stop, Pause or Restart services.
  • Specify service parameters when applicable.
  • Automatic: The service starts at system login.
  • Automatic (Delayed): The service starts a short while after the system has finished starting up. This option was introduced in Windows Vista in an attempt to reduce the boot-to-desktop time. However, not all services support delayed start.
  • Manual: The service starts only when explicitly summoned.
  • Disabled: The service is disabled. It will not run.
  • Change the user account context in which the service operates.
  • Configure recovery actions that should be taken if a service fails.
  • Inspect service dependencies, discovering which services or device drivers depend on a given service or upon which services or device drivers a given service depends.

Obviously, these services make work efficient and convenient, which is why a service for the Node.js application is required.

Node.js Application as a Service

Configuring an application as a service has many advantages. The Node.js application starts on boot and continuously runs for as long as your system is ON. For running the Node.js application as a service, you require an additional package. The package will perform the actual job of starting and re-starting your app.

One such package is the ‘Node-windows’ npm. This package, helps you run the Node.js application as a service.

The node-windows npm module

The node-windows module has a utility to run Node.js scripts as Windows services. Install node-windows with npm. The recommended way is by using the global flag (-g), the reason being, when you use the global flag (-g) you do not need to install node-windows for each new project in the system you are using.

    npm install -g node-windows

It is certainly possible to use node-windows without the global flag as well.

     npm install  node-windows

Please note that like all Windows services, creating this requires administrative privileges. To create a service with node-windows, prepare a script like

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

The above code is to create a new Service object, provide a pretty name and description as well. The script attribute identifies the Node.js script which should run as a service (In this case helloworld.js). After running, the script will be visible from the Windows Services utility as shown below.

node windows

 

Follow the above steps to configure and run the Nodejs Application as a service on windows using node-windows. For more information on NodeJs and MEAN, please follow this blog for more such topics.

 

Software Engineer

No.52, 2nd Floor, Dr.Radhakrishnan Salai,
Mylapore – Chennai – 600 004.
(Opp to Chennai Citi center)
p.charantej@bestirtech.com
 Facebook Icon

Leave a Reply

Your email address will not be published. Required fields are marked *