我刚刚开始使用Node.js,我不知道如何获得用户的投入。 我正在研究 p版功能input(
)或C功能gets
。 感谢。
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.
我刚刚开始使用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);
});
也可以这样做:。 在使用外部世界纳米模块时,也更加安全。 不再需要使用警钟。
@Willian的最新答复。 它将与Aync/await syntax和ES6/ES7合作。
const readline = require( readline );
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
// Usage inside aync function do not need closure demo only
(async() => {
try {
const name = await prompt("What s your name: ");
// Can use name for next question if needed
const lastName = await prompt(`Hello ${name}, what s your last name?: `);
// Can prompt multiple times
console.log(name, lastName);
rl.close();
} catch (e) {
console.error("Unable to prompt", e);
}
})();
// When done reading prompt, exit program
rl.on( close , () => process.exit(0));
如果您想要使用电子仪器(import
,而不是require<
):
import * as readline from node:readline/promises ; // This uses the promise-based APIs
import { stdin as input, stdout as output } from node:process ;
const rl = readline.createInterface({ input, output });
const answer = await rl.question( What do you think of Node.js? );
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
资料来源:。
指出这是一个新特点。 从上述来源来看,这似乎如同Node v17.9.1或以上。
这里的其他解决办法要么是合成,要么是使用封条码<>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
这还很好地发挥了作用:
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();
}
在此,我的工作。 如果你不,你可以使用其他东西。
http://stackoverflow.com/a/74250/300213“>aggregate1166877 lin lin,造成ESPIPE和ESGAIN的错误,没有发生两处变化。
这种办法确定了:
let stdin = fs.openSync("/dev/stdin","rs");
const read = function(message) {
fs.writeSync(process.stdout.fd, message + " ");
let s = ;
let buf = Buffer.alloc(1);
fs.readSync(stdin,buf,0,1,null);
while((buf[0] != 10) && (buf[0] != 13)) {
s += buf;
fs.readSync(stdin,buf,0,1,null);
}
return s;
}
这是一种在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);
}
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.
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 ...
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 ...
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 ...
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 ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
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.