nodejs入门(09)-GET/POST请求
来源:互联网 发布:幼儿识字软件下载 编辑:程序博客网 时间:2024/06/14 21:04
1,GET请求内容
2,获取 POST 请求内容
3,源码地址
1,GET请求内容
GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,手动解析后面的内容作为GET请求的参数。
var http = require('http');var url = require('url');http.createServer(function(req, res){ res.writeHead(200, {'Content-Type': 'text/plain'}); // 解析 url 参数 var params = url.parse(req.url, true).query; res.write("网站名:" + params.name); res.write("\n"); res.write("网站 URL:" + params.url); res.end();}).listen(3000);在浏览器输入http://localhost:3000/user?name=seraph&url=http://blog.csdn.net/wzjisking
2,获取 POST 请求内容
POST 请求的内容全部的都在请求体中,http.ServerRequest 并没有一个属性内容为请求体,原因是等待请求体传输可能是一件耗时的工作。比如上传文件,而很多时候我们可能并不需要理会请求体的内容,恶意的POST请求会大大消耗服务器的资源,所有node.js 默认是不会解析请求体的,当你需要的时候,需要手动来做。
/** * Created by boy on 2017/7/10. */var http = require('http');var querystring = require('querystring');var postHTML = '<html><head><meta charset="utf-8"><title>Node.js post 实例</title></head>' + '<body>' + '<form method="post">' + '网站名: <input name="name"><br>' + '网站 URL: <input name="url"><br>' + '<input type="submit">' + '</form>' + '</body></html>';http.createServer(function (req, res) { var body = ""; req.on('data', function (chunk) { body += chunk; }); req.on('end', function () { // 解析参数 body = querystring.parse(body); // 设置响应头部信息及编码 res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'}); if(body.name && body.url) { // 输出提交的数据 res.write("网站名:" + body.name); res.write("<br>"); res.write("网站 URL:" + body.url); } else { // 输出表单 res.write(postHTML); } res.end(); });}).listen(3000);
3,源码地址
http://download.csdn.net/download/wzjisking/9896159
阅读全文
0 0
- nodejs入门(09)-GET/POST请求
- Nodejs GET/POST请求
- NodeJs处理Get请求和Post请求
- nodejs 发起http get post 请求
- #nodejs post或get 请求获取参数
- nodejs路由--get和post请求
- nodejs学习笔记(五)——处理get和post请求
- nodejs之get/post请求的几种方式
- nodejs发起HTTPS,GET,POST请求Face++人脸识别
- nodeJs get post
- GET请求 Post请求
- get请求 post请求
- Get请求 Post请求
- Retrofit2.0的get请求和post请求(入门级)
- nodejs处理POST请求
- NodeJs处理post请求
- nodejs发送post请求
- nodejs request post 请求
- delphi json 发送
- QTableWidget行选中/删除/添加行
- 序列操作 IV(树套树)
- 简单数据库表基本设计-----持续更新吧
- CodeForces
- nodejs入门(09)-GET/POST请求
- Shiro-002
- Java 中关于String的空对象(null) ,空值(empty),空格 的总结
- [LuoguOJ][2330]Kruskal算法裸题:繁忙的都市
- Service详解
- UCOS2_STM32移植过程、系统内核、事件描述(汇总)
- 设计模式--代理模式--Java实现
- 获取iOS控件的隐藏属性
- 综合面试(hr面)常问的问题