假设URL为:http://localhost:8888/select?name=a&id=5
JavaScript代码
- http.createServer(function(request,response){
- var pathname = url.parse(request.url).pathname; //pathname => select
- var arg = url.parse(request.url).query; //arg => name=a&id=5
- console.log("Request for " + arg );
- var str = querystring.parse(arg); //str=> {name:'a',id:'5'}
- var arg1 = url.parse(request.url, true).query; //arg1 => {name:'a',id:'5'}
- console.log("Request for " + arg1 );
- var name = querystring.parse(arg).name; //name => a
- console.log("name = "+name);
- console.log("Request for " + pathname + " received.");
- }).listen(8888);
//querystring.parse(arg) => { name: 'a', id: '5' }
var url = require('url');
var a = url.parse('http://example.com:8080/one?a=index&t=article&m=default');
console.log(a);
//输出结果:
{
protocol : 'http' ,
auth : null ,
host : 'example.com:8080' ,
port : '8080' ,
hostname : 'example.com' ,
hash : null ,
search : '?a=index&t=article&m=default',
query : 'a=index&t=article&m=default',
pathname : '/one',
path : '/one?a=index&t=article&m=default',
href : 'http://example.com:8080/one?a=index&t=article&m=default'
}