Nodejs Implementing Client Login

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.