Node.js GET与POST请求
【摘要】
var http = require('http');
var url = require('url');
createServer();
submitByGet();
submitByPost();
...
var http = require('http');
var url = require('url');
createServer();
submitByGet();
submitByPost();
function createServer() {
http.createServer(function(req, res){
if(req.method.toUpperCase() == 'GET') {
res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
res.write('submit by ' + req.method.toUpperCase() + '\n');
//url.parse后得到的是一个json对象
res.write(JSON.stringify(url.parse(req.url))+'\n');
//虽然说url.parse后是一个json对象,但是用点号('.')取得它的值以后就是一个字符串对象了
res.end(url.parse(req.url).query+'\n');
} else if (req.method.toUpperCase() == 'POST') {
res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
var postData = 'submit by ' + req.method.toUpperCase() + '\n';
//因为post方式的数据不太一样可能很庞大复杂,所以要添加监听来获取传递的数据
req.addListener('data', function(data) {
postData += data;
}).addListener('end', function(data) {
res.write(postData+'\n');
//url.parse后得到的是一个json对象
res.end(JSON.stringify(url.parse(req.url))+'\n');
});
} else {
res.writeHead(404, {'Content-Type': 'text/plain;charset=utf-8'});
res.end("{'errcode':404,'errmsg':'404 网页未找到'}\n");
}
}).listen(8080, function(){
console.log('listen on port 8080...');
});
}
//因为绝大数网络请求都是get请求,所以官方单独对get请求做了个简化版
function submitByGet() {
var urlPath = 'http://127.0.0.1:8080/index.html?' + encodeURIComponent('name=龙神&password=123456');
http.get(urlPath, function(response){
response.setEncoding('utf-8');
console.log('状态码 : ' + response.statusCode);
console.log('response.headers = ' + JSON.stringify(response.headers));
//注意:这里如果不赋空值的话,undefined会被转为string添加进去
var receivedData = '';
response.on('data', function(chunk) {
receivedData += chunk;
}).on('end', function() {
//默认获取的数据是经过encodeURL之后的数据,需要使用decode解码
console.log('receivedRawData : ' + receivedData);
console.log('receivedDecodeData : ' + decodeURIComponent(receivedData));
});
}).on('error', function(e){
console.error(e.message);
});
}
function submitByPost() {
//以键值对的形式构造的变量默认情况下都是一个对象
var sendData = {'name':'chy龙神', 'password':'123456'};
var postData = encodeURIComponent(JSON.stringify(sendData));
//只有post时,才需要Content-Length
var req = http.request({port:'8080', path:'http://localhost:8080/index.html', method:'POST', headers:{'Content-Type':'application/x-www-form-urlencoded'}, 'Content-Length': postData.length}, function(response){
response.setEncoding('UTF8');
console.log('状态码 : ' + response.statusCode);
console.log('response.headers = ' + JSON.stringify(response.headers));
//注意:这里如果不赋空值的话,undefined会被转为string添加进去
var receivedData = '';
response.on('data', function(chunk) {
receivedData += chunk;
}).on('end', function(){
//默认获取的数据是经过encodeURL之后的数据,需要使用decode解码
console.log('receivedRawData : ' + receivedData);
console.log('receivedDecodeData : ' + decodeURIComponent(receivedData));
});
}).on('error', function(e){
console.error(e.message);
});
//write仅对post方法有效
req.write(postData);
//end方法可以和request方法连写,但是write不可以
req.end();
}
- 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
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>get-post-demo</title>
</head>
<body>
<form method="get" action="http://127.0.0.1:8080" >
<input name="name" type="text" value="freddon" />
<input name="password" type="password" value="123456"/>
<input type="submit" value="submit by GET" />
</form>
<form method="post" action="http://127.0.0.1:8080" >
<input name="name" type="text" value="freddon" />
<input name="password" type="password" value="123456"/>
<input type="submit" value="submit by POST" />
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/52523165
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)