basicAuth middleware adds identity authentication functionality to the website. After using this middleware,
Users must enter their username and password when accessing the website. Only after the user has entered their username and password and passed verification can they access the website.
When the username and password entered by the user meet the conditions, the middleware will return true and allow the user to access the website. Otherwise, it will return false and access to the website is not allowed.
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 it more flexible
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 code: