Node.js http basic auth

前端 未结 4 989
遥遥无期
遥遥无期 2020-12-30 05:58

Is it possible to do basic auth in Node.js just like in Apache?

http://doc.norang.ca/apache-basic-auth.html

I know that if using Express or Connect I can add

4条回答
  •  独厮守ぢ
    2020-12-30 06:28

    I think good choice could be http-auth module

    // Authentication module.
    var auth = require('http-auth');
    var basic = auth.basic({
        realm: "Simon Area.",
        file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
    });
    
    // Application setup.
    var app = express();
    app.use(auth.connect(basic));
    
    // Setup route.
    app.get('/', function(req, res){
      res.send("Hello from express - " + req.user + "!");
    });
    

提交回复
热议问题