![node js windows service node js windows service](https://www.bleepstatic.com/content/hl-images/2020/08/04/nodejs-header.jpg)
PM2_HOME value (this path should be accessible to the service user and Here's what we did: Perform environment setup (recommended )? Yes In theory, your next step would be to run the following: $ pm2-service-install $ npm i -g pm2 pm2-windows-service npm-check-updates Sadly, it did not work perfectly out of the box, so here's how we were able to get it running:įirst, you're going to need pm2, pm2-windows-service, and npm-check-updates (more on that last one in a moment). I'm happy to report that this solution is working well for us! Essentially it accomplishes the same goal: running pm2 resurrect but it also gives you that start/stop/restart control in the same interface as all the rest of your system services.
![node js windows service node js windows service](https://www.coretechnologies.com/products/AlwaysUp/Apps/alwaysup-nodejs-created-v8.0.jpg)
It installs an actual Windows Service that will start automatically after a reboot, and which you can start/stop/restart from the Services panel. The latter, pm2-windows-service says in its readme that it was inspired by pm2-windows-startup and it takes a different approach. This doesn't seem super frail to me (certainly a step or three up from our home-rolled solution with task scheduler!) but can we do better? It installs a registry entry that runs on startup and calls wscript.exe (a system app that can run vbscript scripts) and has it run an included vbscript script that then runs the pm2 resurrect command. The former, pm2-windows-startup is much simpler in design, with fewer dependencies but it uses a more blunt approach to get the job done. I'm only linking the latter because that's the one we decided to go with. I don't know when, but at some point their official docs added some links to two community projects pm2-windows-startup and pm2-windows-service. We're using PM2 for the process management, and it has built in functionality for daemonizing and restarting your apps after a system reboot, but only for *nix systems. It's been a few years since we came up with the solution-that-shall-not-be-named, and we figured there's a decent chance that someone figured out something better since then. Recently a coworker and I went back to the drawing board on this. Trust me, the world is a better place if I don't share those detials. We actually did have a way to make it work, but it involved using the Windows Task Scheduler, and a batch file, and some tedious work any time there was a new node app to add to the mix. Gotta love em, right? But just because we have some constraints to work around doesn't necessarily mean we can't have nice things.Īs my team transitions to writing more and more of our application as node.js powered microservices, we were starting to feel the pain of not having a good solution for starting things up automatically after an OS reboot. The Digital Garden of Adam Tuttle TIL: Running Node.js Apps as a Windows ServiceĪh, legacy servers. This service can be automatically started when the server restarts.TIL: Running Node.js Apps as a Windows Service
Node js windows service install#
Luckily there is a NPM package node-windows which can install a node application as a Windows service. Every time the server restarted, the internal API was down and had to be manually started. Unfortunately I did not find a reliable way to start PM2 whenever the Windows Server restarts. PM2 can manage the node process and keep it up and running. We need an alternative to keep the node process running to make the internal API available on the server via localhost. In this case, we can't rely on IIS to manage the node process for us as IIS would expose the web application to the internet. The internal API is only consumed by other applications running on the same server. I have built an internal API in the past which should not be accessible from the outside for security reasons. In some cases however we don't want to expose the node.js web application via IIS. When it receives a request it will start the node process and pass the web request for node to handle it. With the help of the HTTP platform handler, IIS manages the node process for us.
![node js windows service node js windows service](https://kinsta.com/wp-content/uploads/2021/03/Nodejs-Architecture.png)
In the last article a node web application was deployed on a Windows server. Using HTTP platform handler to host a node.js application via IIS.Hosting a Node.js application on Windows with IIS as reverse proxy.These are the previous articles on the topic:
Node js windows service how to#
It wasn't my intention when I started the first article but this has become a series of how to run node applications in production with IIS on Windows.