假设URL为:http://localhost:8888/select?name=a&id=5

JavaScript代码
  1. http.createServer(function(request,response){    
  2.         var pathname = url.parse(request.url).pathname;  //pathname => select    
  3.             
  4.         var arg = url.parse(request.url).query;          //arg => name=a&id=5    
  5.         console.log("Request for " + arg );    
  6.         var str = querystring.parse(arg);                //str=> {name:'a',id:'5'}    
  7.             
  8.         var arg1 = url.parse(request.url, true).query;   //arg1 => {name:'a',id:'5'}    
  9.         console.log("Request for " + arg1 );    
  10.             
  11.         var name = querystring.parse(arg).name;         //name => a    
  12.         console.log("name = "+name);    
  13.     
  14.         console.log("Request for " + pathname + " received.");    
  15.     }).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'
}

Tags: ,
NodeJs平台 | 评论(0) | 引用(0) | 阅读(4257)