How to stop WSH execution while working with node. - node.js

I am running forllowing test.js file using
>node test.js
test.js is simple code
var fs = require("fs");
fs.readFile('2.rtf', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended");
It shows error
so naturally I remove the extension and ran following command which worked fine.
node test
I am running windows 7 32 bit. How to stop WSH while using node commnad?

The problem seems to be that you have a file named node.js in the same directory and due to the way Windows resolves commands, it's trying to execute the file node.js (using the default handler for that file type, which is typically JScript) instead of node the node.js executable. Rename the node.js file to something else and it should work.

Related

child_process in Node js Google App Engine

I have a Node Express server that works on localhost. It uses child_process to run a C++ standalone executable.
The code that uses child_process is the following (the application creates output.txt):
app.post('/generate', async function(req, res)
{
var input1 = req.body.input1;
var input2 = req.body.input2;
var execFile = require('child_process').execFile;
var program = "path/to/executable";
var args = [input1, input2];
var child = execFile(program, args,
function (error, stdout, stderr){
console.log(error);
console.log(stdout);
console.log(stderr);
const file = __dirname + "/output.txt"
app.get('/output.txt', function (req, res) {
res.sendFile(path.join(__dirname + '/output.txt'));
});
res.send("/output.txt");
})
})
This works locally.
I'm now trying to deploy it on Google Cloud Platform with App Engine.
However, when I go the website that I host, and launch this POST /generate request, I don't get the expected output. In the Google Cloud Platform logs of my project I can see the following error:
textPayload: "{ Error: spawn cpp/web_cpp_app/x64/Debug/web_cpp_app ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
at onErrorNT (internal/child_process.js:415:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
"
At first I didn't understand the error, but now I can see that if I locally run the same project, but set the path of the standalone executable to an invalid path, I get the same error. I'm guessing that when I deploy, my executable is somehow not included?
Is there something specific I need to add in package.json or app.yaml files, to include the executable?
EDIT:
Could it be that the app engine runs on Linux, and my executable is for Windows?
ENOENT means "no such file or directory", so your path could be wrong, or the container doesn't recognize the program as executable.
But either way, you will need to build and include a linux-compatible binary of your child_process program in your project directory when you deploy. You could build this manually, or use something like Cloud Build to compile it in a container that's identical to that of App Engine.
You are right about the OS, per this Doc Appengine standard for NodeJS uses Ubuntu OS, and flexible uses Debian
About the executable compatibility I found this post

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.

nodeJS:fs.write callback and fs.writeFile not working

I knew nothing about fs until I was learning to use casperjs to scrape some content from a website and save them to a file. Following some examples on the web, I write this file scrape.js (The json data has been tested so it has nothing to do with the issue):
var fs = require('fs');
var url = "http://w.nycweb.io/index.php?option=com_k2&view=itemlist&id=4&Itemid=209&format=json";
var casper = require('casper').create();
casper.start(url,function(){
var json = JSON.parse(this.fetchText('pre'));
var jsonOfItems={},items = json.items;
items.forEach(function(item){
jsonOfItems[item.id] = item.introtext.split('\n');
})
fs.write('videoLinks.json',JSON.stringify(jsonOfItems),function(err){
if (err) console.log(err);
console.log('videoLinks.json saved');
})
});
casper.run();
When I do casperjs scrape.js in command line of my Ubuntu 14.04 server, I won't see the file saved message as expected, although the file is properly saved. So this is the first question: why the callback isn't running at all?
Secondly, I also tried fs.writeFile, but when I replace fs.write with it, the file isn't saved at all, nor is there any error information.
I do notice that in casper documentation it's said that casper is not a node.js module and some module of node.js won't be available, but I doubt it has anything to do with my issues. And I think it worths to mention that previously when I run this script I only get a respond like
I'm 'fs' module.
I had to follow this question to reinstall fs module globally to get it working.
fs.write expects a file descriptor where you are trying to give it a filename. Try fs.writeFile. https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback
Edit: Oh you tried that. Are you sure it didn't write it somewhere like the root directory? Tried a full path in there?
And what version of node are you running?

Assistance with Node.js functions integration in Apache 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

phantomjs cannot find module webpage in node's child process

I'm trying to run phantomjs in a node child process. Phantomjs complains it can not find module 'webpage'. Running the script from the cli works fine:
phantomjs script.js
I stripped the problem down to these two files:
main.js:
var script = require('./script');
var cp = require('child_process');
cp.exec('/usr/bin/phantomjs script.js');
script.js
var page = require('webpage').create();
page.open("http://www.google.com", function(status) {
console.log("opened google? ", status);
var title = page.evaluate(function(s) {
return s;
}, 'Hello');
console.log(title);
phantom.exit();
});
Running the following command fails:
node main.js
with error:
module.js:340
throw err;
^
Error: Cannot find module 'webpage'
I've tried to specify the working directory in cp.exec as an option without effect. Is there a way to set the node search path for modules in a global context so it can be found in a new node process? Or am I doing something else wrongly?
PhantomJS is not a node.js module and cannot be directly used from node. I think you understand this, because you try to invoke it via child_process.
The problem is the line var script = require('./script');. As I understand, it is the phantomjs script. Since node.js doesn't have a webpage module, but phantomjs does the require fails and everything with it. Simply remove the line. It doesn't seem like you use it anyway.

Resources