Express middleware basicAuth _ node. js

Source: Internet
Author: User
This article mainly introduces how to use the express middleware basicAuth in node. js. If you need it, you can refer to the basicAuth middleware to add the identity authentication function for the website. After using this middleware,

The user must enter the user name and password when accessing the website. The user can access the website only after entering the user name and password and passing verification.

When the username and password entered by the user meet the conditions, the middleware returns true to allow the user to access the website. Otherwise, false is returned. The user is not allowed to access the website.

The Code is as follows:


Var express = require ("express ");
Var app = express ();
App. use (express. basicAuth ("gys", "123 "));
App. get ("/", function (req, res ){
Res. send ("Hello ff ");
});
App. listen (1337, "127.0.0.1", function (){
Console. log ("start listening, haha ");
});



Modify the code to make the code more flexible

The Code is as follows:


Var express = require ("express ");
Var app = express ();
App. use (express. basicAuth (function (user, pass ){
Return user = "gys" & pass = "123 ";
}));
App. get ("/", function (req, res ){
Res. send ("Hello ff ");
});
App. listen (1337, "127.0.0.1", function (){
Console. log ("start listening, haha ");
});

Run the Code:

Related Article

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.