Node JS提示库无法隐藏密码

3
我将使用Node.js的prompt库,下面是我的代码:
```javascript ```
请注意:这段代码涉及IT技术。
// Entry point for the program
var prompt =require ('prompt' )
var Basic = require('./helper/basic')
var program = require('./helper/cli-args')

var pwd = new Basic()

var promptSchema = {
  properties: {
    sprintID: {
      description: "Enter sprint ID",
      type: 'integer'
    },
    password: {
      description: "Enter the password for " + program.user,
      hidden: true
    }
  }
}
prompt.start()
prompt.get(promptSchema, function (err,result) {
  if (err) console.log(err)
  program.sprint = result.sprintID
  pwd.setDigest(result.password)
  prompt.stop()
  console.log ("Sprint ID: ", program.sprint)
  console.log("Basic: ", pwd.digest);
})

基础类非常简单:
// Basic authentication
var base64 = require('base-64')
var program = require ('../helper/cli-args')

class Basic {
  setDigest(pwd) {
    this.digest = base64.encode(program.user.concat(":").concat(pwd))
  }
}
module.exports = Basic

我遇到的问题是,提示框无法隐藏密码。 这是输出结果: $ npm start jira@1.0.0 start D:\Documents\Programmation\NodeJS\jira node index.js prompt: Enter sprint ID: 156 prompt: Enter the password for sikkache: Not the real Password Sprint ID: 156 Basic: c2lra2FjaGU6Tm90IHRoZSByZWFsIFBhc3N3b3Jk
正如您所见,密码是明文显示的。
有没有人可以帮我,我非常需要将密码隐藏起来。
1个回答

2
你可以尝试使用 readline-sync 模块,它有一个名为 hideEchoBack 的选项,可以用 * 号隐藏屏幕上输入的文本。
var readlineSync = require('readline-sync');

// Wait for user's response. 
var userName = readlineSync.question('May I have your name? ');
console.log('Hi ' + userName + '!');

// Handle the secret text (e.g. password). 
var favFood = readlineSync.question('What is your favorite food? ', {
  hideEchoBack: true // The typed text on screen is hidden by `*` (default). 
});
console.log('Oh, ' + userName + ' loves ' + favFood + '!');

无法在gitbash上运行(我正在使用Windows,真丢人),但在powershell上可以运行。 - Sélim Ikkache

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接