Node.js as service, exec doesn't work - node.js

I'm running Node.js project as service using nssm. When user clicks button on my nodejs website it should run
require('child_process').exec('cmd /c batfile.bat', function({ res.send(somedata); });
but instead it just skips running bat file and jumps to res.send(somedata). Why is that?
When I run Node.js using cmd and npm start server.js it works fine. How can I make exec work while running nodejs as service?
Edit, some code:
require('child_process').exec('cmd /c batfile.bat', function(){
var log = fs.readFileSync('logs/batlog.log', 'utf8');
var html = fs.readFileSync('logs/batlog.htm', 'utf8');
var json = {"log": log, "html": html};
res.send(json);
});
and the batfile.bat is supposed to generate those 2 files but it just doesn't if I run nodejs as service.

Related

nodejs writeFile only working with admin permssions

I currently learning electron and I would like to write a config file in the same dir than the exe file. This is my code:
var save = fs.readFileSync(__dirname+"/config.json")
save = {
storein : savegame
}
fs.writeFile("./config.json", JSON.stringify(save), function(error){
//other stuff
})
But if I run this without administrator permissions, its not working, only if I run the .exe as an admin. If I use electron app it works too

How to run apps in the background using pm2 from a nodejs script?

I am trying to create an app that allows me to create demos of my other apps and run them on subdomain. Now everything works when I run my app through node server.js command, but when I run it in the background using pm2 start server.js, The following script doesnt work. When I log into my droplet using SSH, and run pm2 status, the demo app's start script is not on the list. What should I do?
const runInitialServerConfigurationNodejs = async (subdomain, port) => {
await shellPromise(`cp /var/www/${subdomain}_app/server/.env.example /var/www/${subdomain}_app/.env`);
await shellPromise(`cd /var/www/${subdomain}_app/server && yarn`);
await shellPromise(`mv /var/www/${subdomain}_app/server/server.js /var/www/${subdomain}_app/server/${subdomain}.js`);
await shellPromise(`echo "APP_ENV=production" >> /var/www/${subdomain}_app/server/.env`);
await shellPromise(`echo "MONGODB_KEY=mongodb://localhost:27017/${subdomain}_db" >> /var/www/${subdomain}_app/server/.env`);
await shellPromise(`echo "NODE_PORT=${port}" >> /var/www/${subdomain}_app/server/.env`);
await shellPromise(`cd /var/www/${subdomain}_app/server && pm2 start ${subdomain}.js && pm2 save && pm2 set pm2:autodump true`); // doesnt work here
}
Somehow pm2 does not work for my implementation. However i managed to get it working using the forever module.

Can't get os-service app working except from command line

I have a node.js app that I want to run as a windows service, and I'm using os-service to try to achieve that.
The service installs seemingly correctly, and the Windows Services management console provides this info about it:
In the listing:
Name: wibble
Description: (blank)
Status: (blank)
Startup Type: automatic
Log On As: Local System
In the properties/General pane:
Path to executable:
"C:\Program Files\nodejs\node.exe" "C:\path\to\app.js" "--run"
Attempting to start this service using powershell thus
Start-Service wibble
produces an error message containing: Cannot start service wibble on computer '.'
Attempting to start this service from the management interface's "Start the Service" link with the service selected in the Services manager yields a different error message: Windows could not start the wibble service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion.
The following works, when executed from a powershell in same folder as the app. Clients can connect to and use the app successfully:
node app.js --run
However, the powershell stops at that point until CTRL+C stops the service.
Here's the JS I added to the app to get it to run as a service:
var service = require('os-service');
var fs = require('fs');
process.chdir(__dirname);
if (process.argv[2] == '--run') {
var fn = process.env.LOCALAPPDATA + '/path/to/wibble.log';
var logStream = fs.createWriteStream (fn);
service.run (logStream, function () {
'use strict';
service.stop (0);
});
// rest of app here
}
module.exports = app
Hopefully I've overlooked something dumb. My gratitude if you can spot it!

Run Node.js server file automatically after launching Electron App

Im using GitHub Electron to create Desktop application with web technologies.
I'm using Node.js as the server, my problem is that i don't know how to run the file server.js just when launching the electron app.
I want to package my app for distribution so that i can run the server without the command line $ node server.js.
Just simply require the server.js file in your main file (e.g. app.js):
var app = require("app")
, server = require("./server")
;
...
And in the server.js file you can have:
require("http").createServer(function (req, res) {
res.end("Hello from server started by Electron app!");
}).listen(9000)

nodeJS/ test my js file

I wrote the next try.js file
var net = require('net')
function create(r){
var server = net.createServer(function(socket) {
socket.write('hello\n');
socket.end('world\n');
});
server.listen(8000);
}
and I want to check if this function works well. So I want to excute it, and then go to localhost:8000 and check if I get the Hello massage. I try to go to dictionary of the try.js file via the node console, but I don't see any option for to do this.
the way you execute a nodejs app is by commandline
node try.js
then you can navigate to localhost:8000 but what you made there arnt a http server so browsers arnt going to get the response. telenet should give you the response 'hello world'. but more likely youre after the http insted of net server

Resources