Node JS提示库不隐藏密码

时间:2017-04-02 15:47:57

标签: node.js properties hidden prompt

我正在使用Node.js的提示库,我有这个代码:

// 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类非常简单:

// 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启动D:\ Documents \ Programmation \ NodeJS \ jira   node index.js

     

提示:输入sprint ID:156   提示:输入sikkache的密码:不是真实密码   冲刺ID:156   基础:c2lra2FjaGU6Tm90IHRoZSByZWFsIFBhc3N3b3Jk

如您所见,密码清晰。

任何人都可以帮助我,我真的需要隐藏密码。

1 个答案:

答案 0 :(得分: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 + '!');