Guest User

Untitled

a guest
Aug 31st, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. Basic HTTP authentication in Node.JS?
  2. var http = require('http');
  3. http.createServer(function (req, res) {
  4. res.writeHead(200, {'Content-Type': 'text/plain'});
  5. res.end('Hello Worldn');
  6. }).listen(1337, "127.0.0.1");
  7.  
  8. http.createServer(function(req,res){
  9. var header=req.headers['authorization']||'', // get the header
  10. token=header.split(/s+/).pop()||'', // and the encoded auth token
  11. auth=new Buffer(token, 'base64').toString(), // convert from base64
  12. parts=auth.split(/:/), // split on colon
  13. username=parts[0],
  14. password=parts[1];
  15.  
  16. res.writeHead(200,{'Content-Type':'text/plain'});
  17. res.end('username is "'+username+'" and password is "'+password+'"');
  18.  
  19. }).listen(1337,'127.0.0.1');
Add Comment
Please, Sign In to add comment