node.js对URL进行解析

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

var server = http.createServer(function(req,res){
	//url.parse()可以将一个完整的URL地址,分为很多部分:
	//host、port、pathname、path、query
	var pathname = url.parse(req.url).pathname;

	//url.parse()如果第二个参数是true,那么就可以将所有的查询变为对象
	//可以打点得到这个参数
	var query = url.parse(req.url,true).query;

	var sex = query.sex;
	console.log("pathname:"+pathname);
	console.log("sex:"+sex);

	res.end("pathname:"+pathname);
});

server.listen(3000,"localhost");

猜你喜欢

转载自blog.csdn.net/aganliang/article/details/88209976
今日推荐