Skip to content

Instantly share code, notes, and snippets.

@kcak11
Last active June 3, 2024 09:14
Show Gist options
  • Select an option

  • Save kcak11/7da7eddf99d2e342dbfa4efa17b118c7 to your computer and use it in GitHub Desktop.

Select an option

Save kcak11/7da7eddf99d2e342dbfa4efa17b118c7 to your computer and use it in GitHub Desktop.
NodeJS Input Text and Password from Terminal

NodeJS Password Input

Implemented using MutableStdOut Writable

//#!/usr/bin/env node
const 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; // Default: Make the output stream visible
var rl = readline.createInterface({
input: process.stdin,
output: mutableStdout,
terminal: true
});
rl.question("Enter username: ", (uname)=>{
rl.question("Enter password: ", (pass)=>{
mutableStdout.muted = false; // Make the output stream visible again for further inputs
rl.clearLine(mutableStdout, 0);
//console.log(); // An alternate for rl.clearLine(mutableStdout, 0);
rl.question("Enter age: ", (age)=>{
rl.question("Gender: ", (gender)=>{
logData(uname, pass, age, gender);
rl.close();
});
});
});
mutableStdout.muted = true; // Hide the output stream while entering password
});
var logData = function() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment