Node.js GET/POST请求(12)
12、Node.js GET/POST请求
获取GET请求内容
由于GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,因此你可以手动解析后面的内容作为GET请求的参数。node.js中url模块中的parse函数提供了这个功能。1
2
3
4
5
6
7var http = require('http');
var url = require('url');
var util = require('util');
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(util.inspect(url.parse(req.url, true)));
}).listen(3000);
在浏览器中访问http://localhost:3000/user?name=w3c&email=w3c@w3cschool.cc
然后查看返回结果: