I am still new to nodeJs, and I am trying to create a stream of inputs but my code is not working once I launch the app in the terminal by calling node fileName .
My input format is like this:
- N the number of queries.
- second line represent a string containing two words separated by a space.
- N lines of a string containing two words separated by a space.
for some reason, the terminal doesn't show a thing.
This is my code :
'use strict';
const fs = require('fs');
var n = 0;
var position = '';
var destination = '';
var array = [];
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = parseInt(readLine(), 10);
const input= readLine().split(' ');
position = input[0] ;
destination = input[1];
console.log('our number is',s, 'and our position and destination:', position, destination);
ws.end();
}
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);this is also throwing an error. You should just write to stdout when you receive data and use shell io redirection to output to a file.