Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Basic HTTP authentication in Node.JS?
- var http = require('http');
- http.createServer(function (req, res) {
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('Hello Worldn');
- }).listen(1337, "127.0.0.1");
- http.createServer(function(req,res){
- var header=req.headers['authorization']||'', // get the header
- token=header.split(/s+/).pop()||'', // and the encoded auth token
- auth=new Buffer(token, 'base64').toString(), // convert from base64
- parts=auth.split(/:/), // split on colon
- username=parts[0],
- password=parts[1];
- res.writeHead(200,{'Content-Type':'text/plain'});
- res.end('username is "'+username+'" and password is "'+password+'"');
- }).listen(1337,'127.0.0.1');
Add Comment
Please, Sign In to add comment