Get password from input using node.js

前端 未结 4 1395
梦谈多话
梦谈多话 2021-02-02 09:29

How to get password from input using node.js? Which means you should not output password entered in console.

4条回答
  •  深忆病人
    2021-02-02 09:48

    To do this I found this excellent Google Group post

    Which contains the following snippet:

    var stdin = process.openStdin()
      , stdio = process.binding("stdio")
    stdio.setRawMode()
    
    var password = ""
    stdin.on("data", function (c) {
      c = c + ""
      switch (c) {
        case "\n": case "\r": case "\u0004":
          stdio.setRawMode(false)
          console.log("you entered: "+password)
          stdin.pause()
          break
        case "\u0003":
          process.exit()
          break
        default:
          password += c
          break
      }
    })
    

提交回复
热议问题