Node.js GET与POST请求
来源:互联网 发布:淘宝店铺全屏海报尺寸 编辑:程序博客网 时间:2024/05/16 06:04
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();}
<!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 0
- Node.js GET与POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- node.js中get与post请求的处理
- node.js处理get和post请求
- Node.js GET/POST请求简单例子
- node.js中get和post请求
- node.js发送http(get,post)请求
- Node.js基础get post请求
- node接收get与post请求
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- 微信小程序 Node.js (基础十二) GET/POST请求
- Node搭建服务器与GET请求,POST提交表单
- 100天土鸡饲养计划(47)
- js 控制input只能输入数字或其他
- 做软件开发对这几款软件应该不陌生
- Git配置SSH,并Push到GitHub上的相关流程
- 征信通产品原型设计文档
- Node.js GET与POST请求
- 来自公众号的SPRING(10)
- LayoutInflater官方文档
- 虚拟机与Windows文件传输
- 【未完待续】Linux磁盘分区
- Github 简明教程
- spring jpa报错
- winsocket入门学习
- 关于java中方法或者函数,形参,实参的个人理解