I want to hide password input. I see many answers in stackoverflow but I can't verify value if I press backspace. The condition return false.
I tried several solution to overwrite the function but I got an issue with buffer if I press backspace, I got invisible character \b
.
I press : "A", backspace, "B", I have in my buffer this : "\u0041\u0008\u0042" (toString() = 'A\bB') and not "B".
I have :
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("password : ", function(password) { console.log("Your password : " + password); });
clear() operates like the clear shell command in terminal. On Windows, console. clear() will clear the current terminal viewport for the Node.
Unit Testing and Test Driven Development in NodeJS Node. js console is a global object and is used to print different levels of messages to stdout and stderr. There are built-in methods to be used for printing informational, warning, and error messages.
log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.
body. username; var password = sReq. body. password; if (username=='myusername' && password == 'mypassword') { // do something here with a valid login } else { // user or password doesn't match } });
This can be handled with readline by intercepting the output through a muted stream, as is done in the read project on npm (https://github.com/isaacs/read/blob/master/lib/read.js):
var readline = require('readline'); var Writable = require('stream').Writable; var mutableStdout = new Writable({ write: function(chunk, encoding, callback) { if (!this.muted) process.stdout.write(chunk, encoding); callback(); } }); mutableStdout.muted = false; var rl = readline.createInterface({ input: process.stdin, output: mutableStdout, terminal: true }); rl.question('Password: ', function(password) { console.log('\nPassword is ' + password); rl.close(); }); mutableStdout.muted = true;
Overwrite _writeToOutput of application's readline interface : https://github.com/nodejs/node/blob/v9.5.0/lib/readline.js#L291
To hide your password input, you can use :
This solution has animation when you press a touch :
password : [-=] password : [=-]
The code :
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.stdoutMuted = true; rl.query = "Password : "; rl.question(rl.query, function(password) { console.log('\nPassword is ' + password); rl.close(); }); rl._writeToOutput = function _writeToOutput(stringToWrite) { if (rl.stdoutMuted) rl.output.write("\x1B[2K\x1B[200D"+rl.query+"["+((rl.line.length%2==1)?"=-":"-=")+"]"); else rl.output.write(stringToWrite); };
This sequence "\x1B[2K\x1BD" uses two escapes sequences :
To learn more, read this : http://ascii-table.com/ansi-escape-sequences-vt-100.php
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.stdoutMuted = true; rl.question('Password: ', function(password) { console.log('\nPassword is ' + password); rl.close(); }); rl._writeToOutput = function _writeToOutput(stringToWrite) { if (rl.stdoutMuted) rl.output.write("*"); else rl.output.write(stringToWrite); };
rl.history = rl.history.slice(1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With