Home >Web Front-end >JS Tutorial >Express middleware basicAuth detailed explanation_node.js

Express middleware basicAuth detailed explanation_node.js

WBOY
WBOYOriginal
2016-05-16 16:29:091743browse

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.

Copy code 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 monitoring, haha");
});


Modify the code to make it more flexible

Copy code 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 monitoring, haha");
});

Run code:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn