简体   繁体   中英

Printing user input to the console in NodeJs

please I'm having a tough time finding solution to this code, I was asked to print the user input to the console in nodeJs in one of these learning sites. Am a newbie in nodeJs and i don't know how to go about it. Any help with be appreciated. Here is the lines of code. Thanks

 'use strict'; 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.trim().split('\\n').map(string => { return string.trim(); }); main(); }); function readLine() { return inputString[currentLine++]; } function greeting(parameterVariable) { // This line prints 'Hello, World!' to the console: console.log('Hello, World!'); console.log(inputString); } function main() { const parameterVariable = readLine(); greeting(parameterVariable); } 

You first need to open the standard input stream. Try something like this snippet:

// We start listening for input:
const stdin = process.openStdin();
let content = '';

stdin.addListener('data', d => {
  content += d.toString();
});

stdin.addListener('end', () => {
  console.info(`Input: ${content}`);
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM