Basic HTTP authentication in Node.JS?

后端 未结 7 1768
遇见更好的自我
遇见更好的自我 2020-11-28 21:09

I\'m trying to write a REST-API server with NodeJS like the one used by Joyent, and everything is ok except I can\'t verify a normal user\'s authentication. If I jump to a t

相关标签:
7条回答
  • 2020-11-28 21:37

    You can use 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 ...
    });
    
    // Creating new HTTP server.
    http.createServer(basic, function(req, res) {
        res.end("Welcome to private area - " + req.user + "!");
    }).listen(1337);
    
    0 讨论(0)
提交回复
热议问题