我尝试了几种不同版本的node.js,但readline.question似乎从未执行我的回调。有人可以发现我可能在做错什么吗?
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('What do you think of node.js? ', function(answer) {
console.log('Thank you for your valuable feedback:', answer);
rl.close();
});
答案 0 :(得分:0)
/*
* main.js
*/
//
const readline = require('readline');
//
const rl = readline.createInterface({
input: process.stdin,
output:process.stdout
});
//
var question = "What do you think of node.js? \n";
var feedback = "Thank you for your valuable feedback:\n ";
console.log(question);
rl.question('//////////////...ANSWER..//////////////////////\n:',
function(answer){
console.log(feedback, answer);
rl.close();
});
//
/////////////////////////////////////////////////////////////////////
//Your environment should be set up for using Node.js and npm.
/////////////////////////////////////////////////////////////////////
/////////////////...OUTPUT...////////////////////////////////////////
//$ node main.js
//What do you think of node.js?
//
//////////////...ANSWER...//////////////////////
//: Cool. All APIs of Node.js are asynchronous.
//Thank you for your valuable feedback:
// Cool. All APIs of Node.js are asynchronous.
//
////////////////////////////////////////////////////////////////////
//
//
答案 1 :(得分:0)
代码运行正常,我的问题是由于我使用forever
导致的。使用forever
或pm2
之类的流程管理器时,请注意您的流已被修改,因此基础流程可能无法接收输入。