Capture Command line response in an Electron/NodeJS app - javascript

I need to execute command line commands from an ElectronJS App which also has NodeJS available and then capture the response from those commands ran.
The Xampp Windows WAMP Dev Server app shown below has an example of capturing the command response and showing it in an app.
How can I do this in a NodeJS/ElectronJS app?

You can use the module Child Proccess using the method exec. The documantation for it is pretty clear and can be seen in this link: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
The command line response will be in the stdout parameter of the callback function.

Related

Internal Server error using Bash Terminal, any idea why is it throwing this error and how to fix it?

I have just recently started learning about APIs as party of Full stack developer course. Everything has been working fine until I have activated system storage sense in Windows to clear some space in the disk /c, the curl command stopped working and is giving internal server error.
curl calling request error using bash terminal
I have realized there is an issue when I have run my code and it stopped pulling information from the API link.
Since you made a windows change my guess would be - try addding the curl folder path to your Windows PATH environment variable so that the curl command is available from any location at the command prompt.

How to use SIGUSR1 with node.js on Windows

I use git bash on Windows to run commands.
In the doc on debugging they say the following:
If a process was started without --inspect, signal it with SIGUSR1 to
activate the debugger and print the connection URL.
I tried to do that:
$ node index.js
server is listening on 3000
$ ps
PID COMMAND
16348 /c/Program Files/nodejs/node
$ kill -s SIGUSR1 16348
But then the process stops instead of printing the connection URL:
$ node index.js
server is listening on 3000
User defined signal 1
Windows does not support sending signals, but Node.js offers some emulation. Unfortunately, it does not extend to SIGUSR1.
The linked documentation in the original question has been updated to specify that SIGUSR1 only works on Linux and OS X. (Although I'm opening a pull request right now to change it to say that it doesn't work on Windows, rather than that it does work on Linux and OS X. I'm pretty sure it will work, for example, in AIX and SmartOS. There's probably even a test for it in the code base.)

How can i debug a nodejs backend with node-inspector?

I am trying to debug my backend and i am not really sure if i am doing it the correct way.
I have set up the debugging as follows:
Node 6.9.1 and node-inspector 0.12.8
open a command prompt and run the following command:
node-inspector --web-port=3030 (server app port)
open another command prompt and run the following command:
node --inspect --debug-brk server.js
browse to the given URL in the second command prompt log on screen
press F8 to make the server run
eventually put some others breakpoints
browse on another tab to your app (address and port defined in 1-)
see server execution stops on breakpoints defined on step 5.
Now when i run node --inspect app.js and everything looks good so far. I can debug the first start in the app.js. But if I want to debug an endpoint with POSTMAN, I get the error "Cannot POST /api/trainingsWeek". The endpoint works if I don't debug.
Do I have to take another address? Or another tool than POSTMAN?
ANd what is the difference between node --inspect app.js and node-debug
GitHub Issue
UPDATE
This was my stupid mistake :P Here is the solution: https://github.com/node-inspector/node-inspector/issues/907#issuecomment-280620108
I use VS Code to debug. I guess you don't need node-inspector, it will run with the integrated debugger from Node. To get it running, open your project and the debug view. There should be no configuration available. Click on the little wheel -> choose node.js as the environment.
After that the standard json will be created and looks like this:
And then click on the play button with the selection start programm. With that the debugging should run. Now you can use POSTMAN with the same url when you just run your server and the debugger from VS Code should hit your breakpoint.

MONGO_URL for running multiple Meteor apps on one server

I have one Meteor application running on my Ubuntu server (Digital Ocean). I use Meteor Up (MUP) to deploy and keep the app running. Everything works fine.
However, when I try to deploy a second app on the same server, something goes wrong in connecting to the MongoDB. I get a long and unreadable error message that starts "Invoking deployment process: FAILED" and then ends with
Waiting for MongoDB to initialize. (5 minutes)
connected
myapp start/running, process 25053
Waiting for 15 seconds while app is booting up
Checking is app booted or not?
myapp stop/waiting
myapp start/running, process 25114
And the app refuses to run. I have tried a number of things to fix this and will edit this post if more info is requested, but I'm not sure what's relevant. Essentially I don't understand the Error message, so I need to know what the heck is going on?
EDIT:
I want to add that my app runs fine if I go into the project folder and use the "meteor" command. Everything runs as expected. It is only when I try to deploy it for long-term production mode with MUP that I get this error.
EDIT:
I moved on to trying mupx instead of mup. This time I can't even get past the installation process, I get the following error message:
[Neal] x Installing MongoDB: FAILED
-----------------------------------STDERR-----------------------------------
Error response from daemon: no such id: mongodb
Error: failed to remove containers: [mongodb]
Error response from daemon: Cannot start container c2c538d34c15103d1d07bcc60b56a54bd3d23e50ae7a8e4f9f7831df0d77dc56: failed to create endpoint mongodb on network bridge: Error starting userland proxy: listen tcp 127.0.0.1:27017: bind: address already in use
But I don't understand why! Mongod is clearly already running on port 27017 and a second application should just add a new database to that instance, correct? I don't know what I'm missing here, why MUP can't access MongoDB.
It's tricky without your mup.json to see what's going on here. Given what you said, it looks like your 2nd app deployment tries to override/boot mongodb over the 1st one which is locked, the mongodb environment fails to boot, causing then the fail. You should tackle this different ways:
If your objective is to share your mongoDB, point the MONGO_URL from your 2nd mup.jon on your first mongodb instance. It's generally something along the 2701X ports. As it's a shared DB, changes in one database could affect the other.
meteor-up oversees the deployment of your app from a meteor-nice-to-test thing to a node+mongodb environment. You can spawn another mongod instance with :
mongod --port 2701X --dbpath /your/dbpath --fork --logpath /log/path on your DO server and then point MONGO_URL there.
Last but not least, mupx having docker under the hood. Using mupx for your deployments should isolate both apps from each other.

Killing a Node.js program from a shell script

I'm running a node.js application which is acting as a man-in-the-middle proxy to proxy all of the requests I'm making through a PhantomJS headless testing environment. I want to spin up this proxy from either my PhantomJS test script (though I've looked into it and it seems that phantom does not have an exec() command for executing shell commands) or a small shell script which manages both processes. Ideally, it would do something like
#!/bin/bash
node proxy.js
phantomjs runTests.js
kill node process here
Is there any way that I can do this?
Moments after asking this question, I found a much better way to execute the phantom program from within my node app by using a child process. I placed my phantom script within the folder which contained my node app, and then used exec like this:
var exec = require('child_process').exec;
var _phantom = exec('phantomjs runTests.js',function(error,stdout,stderr){
console.log(stdout);
};
_phantom.on('exit',function(code,sig){
process.exit(code);
});
This means I could spin up my proxy server, then execute the child process. The _phantom.on('exit') block allows me to detect when the process exits on a code. Then, it's very simple to signal the node app to quit, using process.exit.

Categories