/**
* FR:海涛高软(QQ技术交流群:386476712)
*/
var http = require("http");
var query = require("querystring");
var url = require("url");
//测试网址为: http://127.0.0.1:3005/xuhaitao/haihuan?name=xuhaitao&age=33
var server = http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/html;charset=utf-8"});
console.log("xx u");
if (req.method.toLowerCase() == "get") { //Get请求
console.log(url.parse(req.url).pathname); //输出结果: /xuhaitao/haihuan
console.log(url.parse(req.url).query); //输出结果: name=xuhaitao&age=33
console.log(url.parse(req.url, true).query); //输出结果: { name: 'xuhaitao', age: '33' }
console.log(url.parse(req.url, true).query["name"]); //输出结果: xuhaitao
console.log(url.parse(req.url, true).query.name); //输出结果: xuhaitao
res.write(JSON.stringify(url.parse(req.url, true).query)); //轉化爲json字符串
res.end();
} else { //Post请求
var shuju = "";
req.addListener("data", function (postchunkk) {
shuju += postchunkk;
});
req.addListener("end", function () {
var ss = query.parse(shuju);
console.log(ss); // { username: 'xuhaitao', pwd: '8855123' }
console.log(ss.username); //取到用户名的内容
console.log(ss.pwd); //取到密码框中的内容
res.end(JSON.stringify(ss)); //json字符串的形式返回给客户端 {"username":"xuhaitao","pwd":"8855123"}
})
}
}).listen(3005, "127.0.0.1");
FR:海涛高软(QQ技术交流群:386476712)