child_process module not returning directory - node.js

I'm doing this tutorial on NodeJS: http://www.nodebeginner.org.
Here is the code I find confusing:
var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
var content = "empty";
exec("ls -lah", function(error, stdout, stderr) {
response.writeHead(200, {"Content-type":"text/plain"});
response.write(stdout);
console.log(stdout);
response.end();
});
}
I have a router that passes the http response to a request handler that calls the start function. This is happening without a problem. However, the stdout parameter is not returning anything in the browser or in the console. I understand that ls -lah is supposed to give a list of files in the current directory. I have 5 other files in my directory, but nothing is being returned. Any ideas on what is happening here?

Related

How can I exec the ping command in nodejs on lambda?

This runs locally and returns the ping output:
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
}
execute("ping -c 3 localhost", function(name){
console.log(name);
});
Running this in lambda it completes but I never see output:
exports.handler = (event, context, callback) => {
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
}
execute("ping -c 3 localhost", function(name){
console.log(name);
});
};
How do I get it to show output?
Sadly there is no way to do ICMP pings from inside AWS Lambda currently - the main issue is that the container environment that Lambdas run inside lacks the CAP_NET_RAW capability needed to allow an application to use raw sockets.
There's no way around this, even trying to use the command line ping utility inside the Amazon Linux container the Lambda runs inside of won't work.
Source:https://github.com/jethrocarr/lambda-ping.
They also proposed a solution you can try.

Execute a cgi script from a Node Express server

So I have a website with a member area. That member area is managed through a payment processor called CCBill. In order for CCBill to manage a password file on my server, they need to execute a cgi script.
Right now, I've looked at cgi and serve-cgi npm modules. But I'm not sure if they can do what I need. Can anyone help me with this?
My Express Router get function:
router.get('*', function(req, res, next) {
console.log('in');
var mPath = path.join(appRoot, '/cgi-bin' + req.params[0]);
console.log(mPath);
const execFile = require('child_process').execFile;
const child = execFile(mPath, function(error, stdout, stderr) {
if (error) {
console.log(error);
throw error;
}
console.log(stdout);
});
});
Scripts (and other executables) can be invoked with the exec() function:
var exec = require('exec');
exec('/path/to/your/script',
function (stderr, stdout, errorCode) {
// You get here when the executable completes
}
}
EDIT
With newer node.js versions exec() is deprecated, so it's better to use child_process.execFile():
const execFile = require('child_process').execFile;
const child = execFile('/path/to/your/script', [parameters], (error, stdout, stderr) => {
// You get here when the executable completes
});

Piping STDOUT of spawn process into superagent upload

I'm struggling to work out how to take the output of a spawned child process and feed that output into a multipart mime upload.
Here is what I have that as far as I can tell should work
var request = require('superagent');
var spawn = require('child_process').spawn;
var spawned = spawn('echo', ['hello', 'world']);
request.post('http://localhost/api/upload')
.attach('file', spawned.stdout)
.end(function(res) {
console.log("DONE", res);
});
Unfortunately this throws the rather unhelpful Error: socket hang up response from Node.
You're quite close!
Here's the final version which does what you want:
var sys = require('sys')
var exec = require('child_process').exec;
var request = require('superagent');
exec('echo hello world', function(err, stdout, stderr) {
request.post('http://localhost/api/upload')
.attach('file', stdout)
.end(function(res) {
console.log('DONE', res);
});
I'm using exec here, because it's callback function outputs the stdout and stderr streams, which it seems like you want.

How to access command line arguments in a node.js child process using child_process.exec?

I am trying to write a test script in node.js for another node.js script that is executed via command line with arguments. When the script is executed in the terminal the arguments are accessible using process.argv[2], process.argv[3], etc. However, those arguments are not present when the script is executed in the test script using child_process.exec().
target.js
var arguments = {
arg1: process.argv[2],
arg2: process.argv[3]
};
console.log(arguments.arg1);
// This outputs '100' when target.js is executed from terminal
test.js
var cp = require('child_process');
cp.exec('node target.js 100 200',
function (err, stdout, stderr) {
if (err) {
console.log(err);
}
console.log(stdout);
// process.argv[2] is undefined when executed as a child process
});
Any suggestions on how to get the same behavior when executing via child_process as I do when I execute it from the terminal?
Your problem is elsewhere. (Caveat: node 0.6.12)
I ran a test using this as a.js:
console.log(JSON.stringify(process.argv));
And using your launcher below:
var cp = require('child_process');
cp.exec('node a.js 100 200',
function (err, stdout, stderr) {
if (err) {
console.log(err);
}
console.log(stdout);
});
I get identical expected output:
joe#toad:~/src$ node a.js 100 200
["node","/home/joe/src/a.js","100","200"]
joe#toad:~/src$ node b.js
["node","/home/joe/src/a.js","100","200"]

response.write() not working in exec

I am following a node.js tutorial and am making a simple server that will display contents of a directory.
function start(response) {
console.log("Request handler 'start' was called.");
// if I put response.write("blah") here it works
console.log(response);
exec("ls -lah", function (error, stdout, stderr) {
console.log(response);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.write("asdfasdf");
console.log("asdf");
console.log(stdout);
response.end();
});
}
This prints nothing on the browser, but shows up on the console.
I tried putting response.write() outside of the exec callback function, and it shows up perfectly on the browser.
Firefox is reporting that the request is not being set at all, not even the content-type header is being set. If I move that outside the exec callback function, it does get set.

Resources