[英]How to handle backspace while reading from STDIN in Node.js?
我正在嘗試使用此咖啡代碼讀取用戶輸入:
_readEmail = (program, opts, c, u, cb) ->
program.prompt 'email: ', /^.+@.+\..+$/, (email) =>
u.email = email
cb()
但是,退格未正確處理。 它只是作為另一個字符讀取,而不是刪除字符。 有一個簡單的方法來處理這個?
您應該使用readline模塊:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of node.js? ", function(answer) {
// TODO: Log the answer in a database
console.log("Thank you for your valuable feedback:", answer);
rl.close();
});
有關文檔和示例,請參閱http://nodejs.org/api/readline.html或https://sourcegraph.com/github.com/joyent/node/symbols/javascript/lib/readline.js 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.