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: