I have node js project and ı want the convert windows service and I want it to start automatically.my code turns into windows service, but the requests I make do not come without running the program.
var Service = require("node-windows").Service;
const svc = new Service({
name: "Academy Service2",
description: "My Node.js application as a Windows service.",
script: "C:\\Users\\Ata\\Desktop\\Academy_APP\\index.js",
env: {
PORT: 5055,
DB_HOST: "localhost",
DB_PORT: 5432,
DB_DATABASE: "academy",
DB_USER: "postgres",
DB_PASSWORD: "*******",
JWT_SECRET_TOKEN: "abcdndgjdfkdfdkdfgdg",
},
});
svc.on("install", function () {
svc.start();
});
svc.install();
The library itself doesn't provide an explicit option for the config you need. But you can take the help of the command line and the nodeJS itself to do this.
So windows services are manageable from the command line using sc commands
To make changes in startup type you want to use
For reference more details on config command:
So command you want to run is
That's part 1.
Now part 2, how to run this automatically as part of the code.
After you are done with installing the service and before executing svc.start(); is when you should make this change ideally. We will use child_process provided by NodeJS as an inbuilt capability
The simplest thing to do would be
I am pretty sure this will need to happen as admin but can't say for sure.
If you want to ensure its success and log it then some more event listening codes will be needed.