node.js 获取url中的各个参数

如果url为:http://127.0.0.1:8020/?param=10&id=code


1,首先引入模块:

var http = require('http');
var url = require("url");
var querystring = require("querystring");


2,创建服务并获取参数:

http.createServer(function(req,res){
	
	//获取返回的url对象的query属性值 
	var arg = url.parse(req.url).query;
	
	//将arg参数字符串反序列化为一个对象
	var params = querystring.parse(arg);
	
	//请求的方式
	console.log("method - " + req.method);
	
	//请求的url
	console.log("url - " + req.url);
	
	//获取参数param
	console.log("param - " + params.param);
	
	//获取参数id
	console.log("id- " + params.id);
	
}).listen(8020,'127.0.0.1');





总结:
1,node.js获取url参数先引入url和querystring两个模块
2,用url.parse方法将url字符串转化为对象,并获取该对象的query属性(参数列表的字符串)
3,用querystring.parse方法将‘参数列表的字符串’转化为参数列表的对象
4,获取对象中对应参数的值

其他

[我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)

[我的CSDN博客,欢迎交流!](https://blog.csdn.net/m0_38082783)

[微信小程序专栏](https://blog.csdn.net/column/details/18335.html)

[前端笔记专栏](https://blog.csdn.net/column/details/18321.html)

[微信小程序实现部分高德地图功能的DEMO下载](http://download.csdn.net/download/m0_38082783/10244082)

[微信小程序实现MUI的部分效果的DEMO下载](http://download.csdn.net/download/m0_38082783/10196944)

[微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)

[微信小程序实例列表](http://blog.csdn.net/m0_38082783/article/details/78853722)

[前端笔记列表](http://blog.csdn.net/m0_38082783/article/details/79208205)

[游戏列表](http://blog.csdn.net/m0_38082783/article/details/79035621)

原文地址:https://www.cnblogs.com/linewman/p/9918533.html