Lightweight URI manipulation in Javascript
var uri = "HTTP://user:pass@domain.com:6666/somepath?key1=value1&key2=value2#somehash?invalidqs";
var p = uri.parse(uri);
should.equal(p.scheme, 'http');
should.equal(p.port, 6666);
should.equal(p.hash, 'somehash');
should.equal(p.qs, 'key1=value1&key2=value2');
...
// defaults can also be included
// in the case just a 'path' is to be parsed
var p = uri.parse('/somepath', { port: 7777 });
should.equal(p.scheme, 'http');
should.equal(p.host, 'localhost');
should.equal(p.port, 7777);
should.equal(p.path, '/somepath');
...
var uri_parts = {
scheme: 'http'
,host: 'domain.com'
,username: 'user'
,password: 'pwd'
,port: 6666
,path: 'somepath'
,query: {
key1: 9999
,key2: 'xyz'
}
,hash: 'fragment'
};
var result = uri.build(uri_parts);
should.equal(result, 'http://user:pwd@domain.com:6666/?key1=9999&key2=xyz#fragment', 'Got: ' + result);
sudo npm install
npm test