1

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();
}
1
  • 1
    Your code mostly works. You are reading from stdin, which causes the terminal to lock up until EOF. If you are creating a filter program, this is fine. If not, you'll need to open a different ReadableStream. 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. Commented Sep 9, 2018 at 22:39

1 Answer 1

1

You can simplify this whole thing by allowing readline to buffer the input:

'use strict';

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.on('line', line => {
    rl.write(
        line.replace(/\s*$/, '')
    );
});
Sign up to request clarification or add additional context in comments.

3 Comments

just want to ask what do you mean by buffering the input ??
Great question! In your original code, on data, you were += chunks to a string. This is called buffering, when you take a piece by piece and throw it into larger storage---think of video streaming. Readline takes care of this for you, greatly simplifying the code. The stream's internal buffer may be tiny, taking a little data at a time from the source, but a little data isn't meanful, so you build it into a larger part, that is buffering. In this case, we are doing line-buffered io.
To drive this home, imagine a large video, it's unlikely to fit into memory, and the reality is, you wouldn't need to (since the user is only seeing one video frame at a time). If the playback speed is faster than getting data, the video freezes up and has to wait for more data from the stream's source---buffering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.