Introduction
In this blog post, we will walk you through the process of deploying a Node.js site as a Windows service using Qckwinsvc. While Qckwinsvc might not be the ideal solution for large-scale sites (for which we recommend using PM2), it is a quick and effective option for smaller projects and setting up local servers for testing. By following these steps, you’ll be able to run your Node.js application as a Windows service, ensuring that it runs continuously in the background on your Windows PC.
Running your Node site locally in 7 simple steps
Step 1: Verify Node.js Installation
If you’ve gotten to this blog post you probably already have node up and running, but if you’re deploying on a new machine ensure that you have Node.js installed.
Open the command prompt as an administrator and run the following command:
node -v
If you see the version number displayed, it means Node.js is successfully installed. If not you’ll have to install it from their website
Step 2: Navigate to Your Application’s Folder
Using the command prompt, navigate to the folder where your Node.js application code resides. You can use the cd
command to change directories. For example:
cd C:\path\to\your\application
Step 3: Verify you can run your application
To verify that your application runs correctly, execute your run command. This can be found in the package.json folder, or you can manually run your index file with either
npm run start or node index.js
Ensure that your application starts without any errors and is accessible via a web browser at the host and port specified in my case it’s running at localhost:3000
Step 4: Install Qckwinsvc Globally
To install Qckwinsvc globally on windows, execute the following command:
npm install -g qckwinsvc
Step 5: Configure and Run Qckwinsvc
After installing Qckwinsvc, run it by executing the following command in the command prompt:
qckwinsvc
Qckwinsvc will prompt you to enter the name of your new service and adescription, which will be displayed in the services section of your Windows machine. Specify an appropriate name and description for your service.
Step 6: Specify the Node.js Script
When prompted by Qckwinsvc, provide the path to the index.js or app.js
file located in the root of your Node.js application. If you run the app through package.json check there to see what command is running what file. Make sure to enter the full path, including the drive letter. For example:
C:\path\to\your\application\index.js
Step 7: Verify Service Status
Check the services on your PC to ensure that your Node.js service is running. You can do this by following these steps:
- Press
Win + R
to open the “Run” dialog. - Type
services.msc
and press Enter. - Look for your specified service name in the list of services and verify that it is running.
Step 8: Test Your Application
Finally, open a web browser on your local machine and navigate to the appropriate address specified by your application, in our case localhost:3000. Ensure that your Node.js site is accessible and functioning as expected.
Firewall Considerations:
If you need to access your Node.js site from other PCs or devices on the same network, it’s crucial to configure your firewall settings accordingly. Follow these steps:
- Add Node.js to the firewall: Ensure that Node.js is allowed through both public and private networks.
- If your site is still not visible, open the ports on which your site is running in the advanced firewall settings.
Hostname resolution:
If you want to access your site locally through another domain, you can update your hosts file and netsh to map the address and port to a custom domain of your choice. Â Keep in mind each pc connecting to your IP address will also have to update those settings if they want a custom URL.
Conclusion
Congratulations! You have successfully deployed a Node.js site as a Windows service using Qckwinsvc. This approach allows your application to run continuously in the background, ensuring its availability without manual intervention. Remember to consider alternative solutions like PM2 for larger projects where scalability and advanced features and monitoring are required. Happy coding!