I want to run name.js file from the command prompt using node.js and pass the input file and redirect that output in output.txt, I am writing a command for this is node name.js < input.txt | > output.txt
node name.js < input.txt | > output.txt
but this is not working or I am wrong.
name.js look like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var _line = "";
rl.on('line', function(line){
_line += line;
});
rl.on('pause', function(_line){
console.log(_line);
});
I have also tried this in Powershell -Command "command"
EDIT : for example input.txt contain
hello js
hello node
hello world!
now,if i run node name.js < input.txt > output.txt
. i just get return value of console.log()'s " undefined " in output.txt
passing _line to rl.on('pause',function( _line ){} hide the global _line that's why it's giving undefined and cmd command is fine.
there is other way to do this by using process I/O of node.js
function YourData(input) {
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
YourData(_input);
});
read more about readline and process I/O
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.