[英]Printing user input to the console in NodeJs
請在尋找這段代碼的解決方案時遇到困難,我被要求在其中一個學習站點的nodeJs中將用戶輸入打印到控制台。 我是nodeJ的新手,我不知道該怎么做。 任何幫助,不勝感激。 這是代碼行。 謝謝
'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); }
您首先需要打開標准輸入流。 試試下面的代碼片段:
// 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.