简体   繁体   English

在NodeJs中将用户输入打印到控制台

[英]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. 请在寻找这段代码的解决方案时遇到困难,我被要求在其中一个学习站点的nodeJs中将用户输入打印到控制台。 Am a newbie in nodeJs and i don't know how to go about it. 我是nodeJ的新手,我不知道该怎么做。 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}`);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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