nodejs -- basic认证

来源:互联网 发布:极有家 知乎 编辑:程序博客网 时间:2024/06/06 10:42


var http=require('http');http.createServer(function(req,res){    var au=req.headers.authorization;    if(au == undefined){        res.writeHead(401,{                        'content-Type':'text/plain',                        'WWW-Authenticate':'Basic realm="family"'                     });                     res.end('');    }else{        var str=req.headers.authorization;        str = str.slice(6,str.length);        console.log(str);        var str1 =new Buffer(str,'base64').toString();        if(str1 != "cd:123"){            res.writeHead(401,{                'content-Type':'text/plain',                'WWW-Authenticate':'Basic realm="family"'             });             res.end('');        }else {            res.end("aaaaaaaaaaaaa")        }    }}).listen(8888,'127.0.0.1');console.log('server running at localhost:8888 ');


原创粉丝点击