http的东西太多了,需要慢慢消化。现在弄了个服务器,支持响应浏览器的GET,客户端的GET和POST。其中客户端的get,中文参数没弄好,到服务器里面已经是乱码,这个不清楚原因,以后遇到了再说吧,实在不行用post替代。请求的headers参数有点儿多,还没理解。还有就是request模块没试验呢...
服务器:
/**
* url属性
* export interface Url {
href?: string;
protocol?: string;
auth?: string;
hostname?: string;
port?: string;
host?: string;
pathname?: string;
search?: string;
query?: string | any;
slashes?: boolean;
hash?: string;
path?: string;
}
* */
/**浏览器:http://localhost:3001/?name='百度'&url='www.baidu.com'
* 1,req.url: name=%27%E7%99%BE%E5%BA%A6%27&url=%27www.baidu.com%27
* 2,url.parse(req.url, true):
* Url {
protocol: null,//不清楚这里为什么空
slashes: null,
auth: null,
host: null,//不清楚这里为什么空
port: null,//不清楚这里为什么空
hostname: null,
hash: null,
search: '?name=%27%E7%99%BE%E5%BA%A6%27&url=%27www.baidu.com%27',
query: { name: '\'百度\'', url: '\'www.baidu.com\'' },
pathname: '/',
path: '/?name=%27%E7%99%BE%E5%BA%A6%27&url=%27www.baidu.com%27',
href: '/?name=%27%E7%99%BE%E5%BA%A6%27&url=%27www.baidu.com%27'
}
* */
const http = require("http");
const url = require("url");
const util = require("util");
const queryString = require("querystring");
const hostname = "127.0.0.1";
const port = 3001;
const server = http.createServer(function (req, res) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain;charset=utf-8");
if (req.method === "POST") {//
var reqData = {};
req.on("data", function (chunk) {//知道只有一次的写入,所以就这样处理了~
reqData = queryString.parse(chunk.toString());
});
req.on("end", function () {
console.log(reqData);
res.write("网站:" + reqData["name"]);
res.write("\n");
res.write("网址:" + reqData["url"]);
res.end();
});
}
else if (req.method === "GET") {
var params = url.parse(req.url, true).query;
console.log(params);
res.write("网站:" + params["name"]);
res.write("\n");
res.write("网址:" + params["url"]);
res.end();
}
else {
res.end();
}
});
server.on("error", function (error) {
console.log("我是错误信息" + error);
});
server.on("connection", function () {
});
server.listen(port, hostname, function () {
console.log(__dirname);
console.log("服务器运行在 http://" + hostname + ":" + port + "/");
});
get客户端:
/**
* Created by liyanq on 17/3/19.
*
* export interface RequestOptions {
protocol?: string;
host?: string;
hostname?: string;
family?: number;
port?: number;
localAddress?: string;
socketPath?: string;
method?: string;
path?: string;
headers?: { [key: string]: any };
auth?: string;
agent?: Agent | boolean;
timeout?: number;
}
*/
var http = require("http");
var options = {
hostname: "localhost",
port: 3001,
path: "/?name='baidu2'&url='www.baidu2.com'",//这里不能传中文,不知道原因,谁知道请指点下哈~
method: "GET",
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Cookie": "Webstorm-97ccd078=b588c8c8-9e96-432e-a4f7-30f754875a16",
"Host": "localhost:3001",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko)"
}
};
var req = http.request(options, function (res) {
console.log(res.statusCode);
console.log(JSON.stringify(res.headers));
var resData = "";
res.on("data", function (chunk) {//服务器每次write就执行一次data
resData += chunk.toString();
});
res.on("end", function () {
console.log(resData);//中文没打印出来,不知道原因
})
});
req.on("error", function (e) {
console.log("Error:" + e["message"])
});
req.end();
post客户端:
/**
* Created by liyanq on 17/3/19.
*/
var http = require("http");
var queryString = require("querystring");
var postData = queryString.stringify({
name:"百度3",
url:"www.baidu3.com"
});
var options = {
hostname: "localhost",
port: 3001,
path: "",
method: "POST",
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Cookie": "Webstorm-97ccd078=b588c8c8-9e96-432e-a4f7-30f754875a16",
"Host": "localhost:3001",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko)"
}
};
var req = http.request(options, function (res) {
console.log(res.statusCode);
console.log(JSON.stringify(res.headers));
var resData = "";
res.on("data", function (chunk) {//服务器每次write就执行一次data
resData += chunk.toString();
});
res.on("end", function () {
console.log(resData);//中文没打印出来,不知道原因
})
});
req.on("error", function (e) {
console.log("Error:" + e["message"])
});
req.write(postData);
req.end();