The Nodejs API has a process processing object, which process
is a global
(global variable) that provides information about controlling the current node. JS process. As an object, it is always available for the node. JS application, so it is not necessary require()
.
The process provides stdin and stdout for accepting the keyboard input and output stream, below I implemented a console login Simple example, receive console input user name and password to determine whether the login success.
The code is as follows:
var q = "Please enter username:"; var users = { ' admin ': ' 1234 ', ' user1 ': ' 1234 ', ' user2 ': ' 1234 '}var isinputusername = true;v AR username = "";p rocess.stdout.write (q + "\ n");//Receive input from user input process.stdin.on (' data ', (input) = = // The character entered must finally be a carriage return input = input.tostring (). Trim (); Gets a key-value pair in the collection for all keys if (!username) { if (Object.keys (users). indexOf (Input) = = = 1) { //user name does not exist Process.stdout.write (' User name does not exist ' + ' \ n '); Process.stdout.write (q + "\ n"); Username = ""; } else { process.stdout.write (' Please enter password: \ n '); Username = input; } } else { if (input = = Users[username]) { Console.log (' landed successfully '); Username = ""; } else { process.stdout.write (' Password is incorrect, please re-enter password: \ n ');}} );
Execute the JS code in the node environment in the terminal:
Nodejs Implementing Client Login