I need to get the username and the password that a browser has send to my node.js application from the url.
I digged through various documentations and objects but I can't find anything useful. Does anybody know how to do that? Using Authentication header is not an option because modern bowsers don't set them.
https://username:password@myurl.com/
=================
// /\
// ||
// I need this part
Thanks for your help!
The username:password is contained in the Authorization header as a base64-encoded string:
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');
see this post: Basic HTTP authentication in Node.JS?
This is exactly what you're looking for:
http://nodejs.org/api/url.html
If you want to know where to get the URL itself from, it is passed in the request object, also known as "path":
the URL is accessible by the server in the request object :
http.createServer(function(req, res) {
var url = req.url
console.log(url) //echoes https://username:password@myurl.com/
//do something with url
}).listen(8081,'127.0.0.1');