English 中文(简体)
通过Node.js console获得用户投入
原标题:Get user input through Node.js console

我刚刚开始使用Node.js,我不知道如何获得用户的投入。 我正在研究 p版功能input()或C功能gets。 感谢。

最佳回答

你可以使用三种选择。 我将借这些例子走你们:

(Option 1) prompt-sync: In my opinion, it is the simpler one. It is a module available on npm and you can refer to the docs for more examples prompt-sync.

npm install prompt-sync
const prompt = require("prompt-sync")({ sigint: true });
const age = prompt("How old are you? ");
console.log(`You are ${age} years old.`);

(备选案文2) 它是一纸上的另一个单元:

npm install prompt
const prompt = require( prompt );

prompt.start();

prompt.get([ username ,  email ], function (err, result) {
    if (err) { return onErr(err); }
    console.log( Command-line input received: );
    console.log(   Username:   + result.username);
    console.log(   Email:   + result.email);
});

function onErr(err) {
    console.log(err);
    return 1;
}

(Option 3) readline: It is a built-in module in Node.js. You only need to run the code below:

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

rl.question("What is your name ? ", function(name) {
    rl.question("Where do you live ? ", function(country) {
        console.log(`${name}, is a citizen of ${country}`);
        rl.close();
    });
});

rl.on("close", function() {
    console.log("
BYE BYE !!!");
    process.exit(0);
});
问题回答

这里的其他解决办法要么是合成,要么是使用封条码<>prompt-sync。 我想要一种阻塞性的解决办法,但prompt-sync。 我的终点站一贯腐败。

I found a 令人热心的答案是,它提供了一个很好的解决办法。

建立职能:

const prompt = msg => {
  fs.writeSync(1, String(msg));
  let s =   , buf = Buffer.alloc(1);
  while(buf[0] - 10 && buf[0] - 13)
    s += buf, fs.readSync(0, buf, 0, 1, 0);
  return s.slice(1);
};

Use it:

const result = prompt( Input something:  );
console.log( Your input was:   + result);

申斥: Im cross-posting my own response 页: 1

here

这还很好地发挥了作用:

const fs = require( fs );

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( 
 )
        .map(str => str.replace(/s*$/,   ));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10); // Read and integer like this

    // Read an array like this
    const c = readLine().split(   ).map(cTemp => parseInt(cTemp, 10));

    let result; // result of some calculation as an example

    ws.write(result + "
");

    ws.end();
}

在此,我的工作。 如果你不,你可以使用其他东西。

这是一种在Node.js进行超时投入/产出处理的形式。 我们利用标准投入和产出流与用户互动或处理投入和产出数据。 我在黑点问中使用过这个词。

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input; // Reading input from STDIN using `data` event.
});

//the `end `event is used to know when input reading is complete.
process.stdin.on("end", function () { 
   main(stdin_input); //passing a parameter in main function
});


function main(inp){
process.stdout.write(inp);

}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签