Basic HTTP authentication with Node and Express 4

后端 未结 9 2091
萌比男神i
萌比男神i 2020-11-29 15:48

It looks like implementing basic HTTP authentication with Express v3 was trivial:

app.use(express.basicAuth(\'username\', \'password\'));

V

9条回答
  •  没有蜡笔的小新
    2020-11-29 16:42

    I changed in express 4.0 the basic authentication with http-auth, the code is:

    var auth = require('http-auth');
    
    var basic = auth.basic({
            realm: "Web."
        }, function (username, password, callback) { // Custom authentication method.
            callback(username === "userName" && password === "password");
        }
    );
    
    app.get('/the_url', auth.connect(basic), routes.theRoute);
    

提交回复
热议问题