I have to run file test.js which is at different location than the my running application. To do that i have tried the fallowing code
var execFile = require("child_process").execFile;
exports.sync = function(req, res) {
console.log("sync called");
var child = execFile("C:/Users/rhush/Desktop/test", function(error, stdout, stderr) {
if (error) {
throw error;
}
console.log(stdout);
res.send({ status: stdout });
});
};
and my test file is here :
function testing() {
console.log('sync job running');
}
testing();
but i got the error
please correct if i am doing any mistake.
To run a js file using execFile you need to pass node command with file name, Use this one:
var execFile = require("child_process").execFile;
exports.sync = function(req, res) {
console.log("sync called");
var child = execFile("node", ["C:/Users/rhush/Desktop/test.js"], function(error, stdout, stderr) {
if (error) {
throw error;
}
res.send({ status: stdout });
});
};
Related
var exec = require('child process').execFile;
exec('C:\something.exe', function (err, data) {
console.log(err)
console.log(data.toString());
});
You can accomplish this by using the exec function from the built-in child_process module as shown in the NodeJS documentation.
const { exec } = require('child_process');
exec('start program.exe', (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
I am writing some code that lists some functionalities which are run by cmd, How can I enclose cmd executables in my code?
Here is a small example to use child_process.
Execute command in cmd.
const { exec } = require("child_process");
function os_func() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
callback(stdout);
});
}
}
app.get("/", (req, res) => {
console.log("inside get");
var os = new os_func();
os.execCommand('arp -a', function (returnvalue) {
res.end(returnvalue)
});
});
There is a global function i.e:
process.argv
Or if you want to take command-line arguments a lot better try npm package yargs.
I have 2 node file, one node i called it test.js node, which is in root folder, now my another node js file in functions/index.js, i want to run my test node in my index.js file, here i have put my files code of that, can anyone please help me how to get its data ?
test.js (it is in root folder)
const GeotabApi = require('mg-api-js');
const authentication = {
credentials: {
database: '***',
userName: '***',
password: '***'
}
}
const api = new GeotabApi(authentication);
api.call('Get', { typeName: 'Group', resultsLimit: 100 })
.then(result => {
return result;
})
.catch(error => {
return error;
});
I want to call that test.js file to my index.js, i have put my code of index.js file but i am not getting its output
functions/index.js
var childProcess = require('child_process');
function runScript(scriptPath, callback) {
// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;
var process = childProcess.fork(scriptPath);
// listen for errors as they may prevent the exit event from firing
process.on('error', function (err) {
if (invoked) return;
invoked = true;
callback(err);
});
// execute the callback once the process has finished running
process.on('exit', function (code) {
if (invoked) return;
invoked = true;
var err = code === 0 ? null : new Error('exit code ' + code);
callback(err);
});
}
exports.test_groups_list = functions.https.onRequest(async (req, res) => {
runScript('../test.js', function (err) {
if (err) {
res.set({ 'Access-Control-Allow-Origin': '*' }).send({ "status": 0,"msg":err });
} else {
res.set({ 'Access-Control-Allow-Origin': '*' }).send({ "status": 1 });
}
});
});
I'm setting up a new application for my final project. I'm trying to execute an exe file using node.js, but it looks that it's not working right.
I don't get any errors...
var {execFile} = require('child_process');
var executablePath = "C:\\Users\\MyUser\\Desktop\\finalProject\\routes\\api\\testcpp.exe";
const child = execFile(executablePath, ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
var {execFile} = require('child_process');
var executablePath = "C:/Users/MyUser/Desktop/finalProject/routes/api/testcpp.exe";
const child = execFile(executablePath, ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
i am executing the following code in nodejs and want an output like this :
and want an output like this
Running Code
Compiling Code
Executing Code
Done Execution
Done Compiling
Done everything
But the output is like this :
Running Code
Compiling Code
Executing Code
Done Execution
Done Compiling
Done everything
This is a strange behaviour :/
var sys = require('sys');
var exec = require('child_process').exec;
var Q = require('q');
var script_sh = 'bash -c' + ' "echo "hegr" ;"';
function compile(req, res) {
var deferred = Q.defer();
console.log('Compiling Code');
exec(script_sh, function puts(err, stdout, stderr) {
if (err || stderr) {
console.log('Error While Compiling');
console.log(err);
res.send(stderr || err );
return deferred.reject(err);
}
console.log('Done Compiling');
return deferred.resolve();
});
return deferred.promise;
}
function execute(req, res) {
console.log('Executing Code');
var deferred = Q.defer();
exec(script_sh, function puts(err, stdout, stderr) {
if (err || stderr) {
console.log('Error While Execution');
console.log(err || stderr);
res.send(err);
return deferred.reject(err || stderr);
}
console.log('Done Execution');
return deferred.resolve();
});
return deferred.promise;
}
function run_code(req,res) {
console.log('Running Code');
compile(req,res)
.then(execute(req,res))
.then(function() {
console.log('Done everything');
}).fail(function (error) {
error.status = 412;
return ;
});
};
module.exports = run_code;
(function() {
if (require.main == module) {
var req = console.log;
var res = console.log;
res.send = console.log;
run_code(req,res);
}
}());
You're not waiting for compile to resolve before invoking execute, but rather using the return value of execute as an argument to then, which happens to be a resolved Promise;
compile(req,res)
.then(execute(req,res))
The argument to then is evaluated when the body of the function run_code is evaluated, not when compile resolves. To wait for compile to resolve before executing execute, use:
compile(req, res)
.then(execute.bind(null, req, res));
execute.bind(null, req, res) will return a pre-bound function that will execute after compile resolves.
Alternatively,
compile(req, res)
.then(function () {
return execute(req, res);
});