Assistance with Node.js functions integration in Apache Linux - linux

Here is a short overview to help you experts understand the situation I am in - sorry that its too verbose, but it might help resolving this issue:
So I have a Linux machine, and it runs Apache properly.
Under '/var/www/html', I put my project files which are HTML (index.html) , and a javaScript file with utility functions.
httpd runs and everyone can view the content when 'http:///index.html' from their PC's.
I want to run a bash script from my Linux machine by letting the users provide the parameters from the front end user interface.
Reading how to do that, I saw tons of examples of how node.js can do that, so I downloaded Node.JS to my Linux machine, and it can be run from:
"
~/Desktop/node-v4.2.1-linux-x64/bin/node --version
v4.2.1
~/Desktop/node-v4.2.1-linux-x64/bin/npm --version
2.14.7
"
So it seems like its properly installed...
Note: I did not put anything in my Linux path after extracting the node.js tar.gz file.
Now, from my Linux machine, under '/var/www/html' , I have an HTML file, and I created an 'onclick' event to invoke a javascript function, in which I wrote a call to run this bash script which is located in my Linux machine under "/" - here it is:
function start_run(pTopoFile, pEmailAddress) {
var childProcess = require('child_process');
var run_command;
run_command = childProcess.exec('/run.sh ' + pTopoFile + ' ' + pEmailAddress, function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
console.log('Signal received: '+error.signal);
}
console.log('Child Process STDOUT: '+stdout);
console.log('Child Process STDERR: '+stderr);
});
ls.on('exit', function (code) {
console.log('Child process exited with exit code '+code);
});
}
When I run the above I get this error:
"ReferenceError: require is not defined"
Which means that even though Node.js was installed properly (as I showed you above), I cannot access its methods from /var/www/html on my Linux machine ...
Can anyone let me know how to link between the great features that node.js has to my scripts?
I hope that I was clear enough with the info I provided...
Thanks,
Tom

Related

Run Linux command from Angular 4 component

Requirement is to fetch the output of a shell script's after running it from the Angular 4 component at the beginning during compilation i.e. just before the website is launched. I have already gone through the threads in stackoverflow i.e. 49700941 and 41637166.
From the first thread i tried to use the below code, but getting error:
Module not found: Error: Can't resolve 'child_process' in 'app/component ...'
const exec = require('child_process').exec; // Can't resolve 'child_process' error coming from this line
exec('/home/myDir/init_setup.sh', (err, stdout, stderr) => {
if (err){
console.error(err);
return;
};
console.log(stdout);
console.log(stderr);
/**
remaining logics
*/
});
Please let me know if I need to import some library explicitly or not to avoid this error.
The modern browsers opens the webpage in isolated sandbox so they have have no access to clients' computers.
Imagine the damage that could be done if a black hat could run batch script on computer that opens his webpage.
The only way to run the script is to run the desktop application on client's machine.
The example code you provided is Node.js code, the desktop framework that user have to install on his machine and run the code intentionally. There's (fortunately!) no way to run it remotely via webpage.

NativeScript: require('child_process') gives an error when running "tns run ios --emulator"

I have a experiment code like this just to test calling a child process from NativeScript app (myapp/app/views/login/login.js):
var exec = require('child_process').exec;
exec('ls', function (error, stdout, stderr) {
if(stdout){
console.log('stdout: ' + stdout);
}
if(stderr){
console.log('stderr: ' + stderr);
}
if (error !== null) {
console.log('Exec error: ' + error);
}
});
when I test this app with "tns run ios --emulator", it gives an error like this:
file:///app/views/login/login.js:1:89: JS ERROR Error: Could not find module 'child_process'. Computed path '/Volumes/xxxx/Users/xxxx/Library/Developer/CoreSimulator/Devices/392A8058-694B-4A5D-B194-DF935815ED21/data/Containers/Bundle/Application/2822CD65-4E4D-443C-8272-135DB09353FC/sampleGroceries.app/app/tns_modules/child_process'.
My question is: how can I resolve this? Should I do "npm install child_process" on the app's directory? But while I was searching for solutions on Google, I read that it should be naturally included from node_modules...
I find a child_process module in:
/usr/local/lib/node_modules/nativescript/lib/common
but as the error message says, it isn't included when I execute the app with tns command. Could someone tell me what I'm missing?
version info:
npm: 3.10.10
node: 7.2.1
tns: 2.4.2
The child_process that you are seeing is a wrapper in the NativeScript CLI for Node's child_process.
There is no child_process in NativeScript as the concept is not relevant in mobile environments (Android/iOS).
The reason Node JS works cross-platform for example, is because its engine has analogous feature implementation (file-system, http, child process) for each of the supported platforms.
Using node's child_process (installing it explicitly and requiring it) will likely not work, as there is no in-house implementation for mobile devices.
If you would like to perform something in the background, consider using NativeScript's Workers.
http://docs.nativescript.org/angular/core-concepts/multithreading-model.html
Edit:
If there isn't a plugin already that is readily available, you could use the underlying native API to call to the device's shell.
Android: execute shell command from android
iOS (Objective C): Cocoa Objective-C shell script from application?
Docs on the nativescript site are available that should help you in 'translating' objC/Java code to JavaScript, though it is pretty straightforward.
http://docs.nativescript.org/runtimes/android/marshalling/java-to-js
http://docs.nativescript.org/runtimes/ios/marshalling/Marshalling-Overview

Unable to spawn a custom .exe from express.js on Windows

I am using node.js/express.js on windows and I have a command I execute when a user takes a image and uploads up from there phone. Once it is uploaded I run myApp.exe to perform some openCV image processing and I output the updated images to a output directory that is a argument in the command below.
I am able to kick this off from my webapp using child_process.exec, but the performance is 60x slower if I run it at command line by itself. To increase the performance I was hoping to use Spawn, but I don't know if this is an accurate assumption, please let me know if it is not.
var exec = require('child_process').exec;
var child = exec('C:\\opt\\package_v030_package\\myApp.exe
--user="C:\\opt\\package_v030_package\\Phone\\'+file.filename+'"
--mv="C:\\opt\\package_v030_package\\mv\\'+req.body.detectionString+'.bmp"
--outPath="C:\\opt\\package_v030_package\\output"
--outputScaled
--outputScaledOverlaid');
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
console.log('stdout: ' + data);
});
child.on('close', function(code) {
console.log('closing code: ' + code);
//res.json("success")
});
I have tried to kick it off using spawn, but it fails to execute with the following: "error child process exited with code 4294967295". The code is below:
var spawn = require('child_process').spawn;
var cmd = spawn('cmd', ['/s',
'/c',
'C:\\opt\\package_v030_package\\myApp.exe',
'--user="C:\\opt\\package_v030_package\\Phone\\'+file.filename+'"',
'--mv="C:\\opt\\package_v030_package\\mv\\'+req.body.detectionString+'.bmp"',
'--outPath="C:\\opt\\package_v030_package\\output"',
'--outputScaled',
'--outputScaledOverlaid'
]);
cmd.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
cmd.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
cmd.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
It seems I am able to execute just myApp.exe from spawn because when I add any of my arguments it fails. Even when I hard code the variables that I inject. Is there an issue with my arguments or am I spawning myApp.exe incorrectly?
Update 1
I placed the command in a .bat and was able to execute it from node.js using spawn. It does not increase performance which leads me to believe that the decrease in performance is a limitation of node.js on the windows platform.
In addition, I performed a few tests using postman to see if I could optimize the call without anything else happening, but I did not succeed. I will leave this question open in the event this changes and node.js is able to better handle performance of a CPU intensive child process.
Update 2 & Answer
I was able to fix this by placing the command that we run at the command line into a java class taking in the detectionString as a parameter. Then from node I use spawn to kick off the .jar file. This caused the speed to increase significantly and run as if I was running it myself at command line.
I was able to fix this by placing the command that we run at the command line into a java class taking in the detectionString as a parameter. Then from node I use spawn to kick off the .jar file. This caused the speed to increase significantly and run as if I was running it myself at command line.

Using zbarcam from nodejs

I am trying to use zbarcam to monitor the usb cam and shoot back the QR Codes it sees.
I am new to both Zbarcam and Nodejs, but have done a fair amount of research and cannot see what is wrong. I am running this on Ubuntu 12.04 LTS and it works fine from the command line, but when run in a child process in Node it returns nothing.
if I run the following on the command line it works great, sees the QR Codes and shoots back the code:
zbarcam /dev/video0 --prescale=1280x720 -q --raw --nodisplay
but when I run it in a small nodejs script it just hangs, I can see the cam enable, but the response does not route back.
var exec = require('child_process').exec,child;
child = exec('zbarcam /dev/video0 --prescale=640x480 -q --raw --nodisplay', function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
console.log('Signal received: '+error.signal);
}
console.log('Child Process STDOUT: '+stdout);
console.log('Child Process STDERR: '+stderr);
});
child.on('exit', function (code) {
console.log('Child process exited with exit code '+code);
});
I suspect it's the way I start it in Node and how node tracks the stdout.
Thanks to all in advance.
you can use the zbar npm, here's my fork on github https://github.com/flatr0ze/node-zbar with updated readme file
run npm install zbar and follow the readme, it should work fine
here's an Npm-less example: https://github.com/cloudpower/qr2wifi/blob/f28f3bf7062b58c8adca2751faaf4e070fa124b4/index.js
good luck!

Deploy phantomJS to node.js app?

I realize "Running PhantomJS on Heroku" is a related but slightly different question as I am trying to use a node app.
I'm having trouble deploying a casperJS (based on phantomJS) script for a node app. I've tried deploying to Heroku by placing the PhantomJS 64-bit binary in my app's /bin directory, but I get the following error when I try to run PhantomJS:
phantomjs: error while loading shared libraries: libQtWebKit.so.4: cannot open shared object file: No such file or directory
From what I've read this can be solved by installing the QtWebKit library, but Heroku does not have this installed. Is there another hosting provider I could use that will work or a way to install this package on Heroku?
Relevant code:
app.get('/', function(request, response) {
var sys = require('sys')
var exec = require('child_process').exec;
var child;
//works but gives error while loading shared library libqtwebkit.so.4
child = exec("phantomjs --version | tr -d '\n'", function(error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr + '\n');
response.header('Content-Type', 'application/json');
response.send('_testcb(stdout:' + stdout + '\nstderr:' + stderr + ')', 200);
if(error !== null) {
console.log('exec error: ' + error);
}
});
});
I've signed up for beta-testing on Nodester but their documentation is still pretty limited at this point.
EDIT: I was able to get it working by simply copying the lib folder of PhantomJS to the root directory of my node app.
Copy the lib folder of phantomjs to the root directory of your node app
You could also try putting a sym link in bin or sbin
The key is that is has to run from terminal using the same account that node runs on.
Also, node-phantom is a good npm library to utilize phantomjs, once you get it working.

Resources