标题:node.js 获取http url路径中的各个参数 出处:刘新修 时间:Mon, 05 Sep 2016 08:52:40 +0000 作者:刘新修 地址:http://liuxinxiu.com:80/node_url/ 内容: 假设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'} Generated by Bo-blog 2.1.1 Release