forked from csxiaoyaojianxian/JavaScriptStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-GET·POST请求.js
More file actions
53 lines (50 loc) · 1.62 KB
/
Copy path11-GET·POST请求.js
File metadata and controls
53 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var http = require('http');
// 【 GET 请求 】
// http://localhost:8888/user?name=CS逍遥剑仙&url=www.csxiaoyao.com
// 使用 url.parse 解析 URL 中参数
var url = require('url');
var util = require('util');
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
// 解析 url 参数
var params = url.parse(req.url, true).query;
res.write("name: " + params.name);
res.write("\n");
res.write("url: " + params.url);
res.end();
}).listen(8888);
// 【 POST 请求 】
// http://localhost:8080
var querystring = require('querystring');
var postHTML =
'<html><head><meta charset="utf-8"><title>GET/POST</title></head>' +
'<body>' +
'<form method="post">' +
'name: <input name="name"><br>' +
'url: <input name="url"><br>' +
'<input type="submit">' +
'</form>' +
'</body></html>';
http.createServer(function (req, res) {
// 定义变量暂存请求体信息
var body = "";
// 通过req的data事件监听函数,每当接收到请求体数据就累加到变量中
req.on('data', function (chunk) {
body += chunk;
});
// end事件触发后,通过querystring.parse将变量解析为POST请求格式向客户端返回
req.on('end', function () {
// 解析参数
body = querystring.parse(body);
// 设置响应头部信息及编码
res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});
if(body.name && body.url) { // 输出提交的数据,第二次
res.write("name: " + body.name);
res.write("<br>");
res.write("url: " + body.url);
} else { // 输出表单,第一次
res.write(postHTML);
}
res.end();
});
}).listen(8080);