The basicAuth middleware adds identity authentication function to the website. After using the middleware,
When a user visits a website, he or she must enter a username and password. Only after the user has entered the username and password and passed verification can he or she access the website.
When the user name and password entered by the user meet the conditions, the middleware will return true, allowing the user to access the website. Otherwise, false will be returned. Access to the website will not be allowed.
The code copy 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 monitoring, haha");
});
Modify the code to make the code more flexible
The code copy 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 monitoring, haha");
});
Run the code: