Node(6) Accessing GET and POST data using http.ServerRequest
来源:互联网 发布:淘宝小店怎么加入一淘 编辑:程序博客网 时间:2024/06/01 10:03
http.ServerRequest or simply request contains information from GET or POST
Get objects are in url
querystring.parse() turn the post data into an object
GET
GET example:
var http = require( 'http' );var url = require( 'url' );var util = require( 'util');var querystring = require( 'querystring' );var server = http.createServer( function( request, response ){if( request.method === 'GET' ){//parse the urlvar parts = url.parse( request.url )//write the GET details to responseresponse.writeHead( 200, {'content-type': 'text/plain'});response.write( 'path => ' + parts.path + '\n');response.write( 'query => ' + parts.query + '\n');response.write( 'foo => ' + querystring.parse( parts.query ).foo + '\n' );response.end('Done!');}});server.listen( 9000 );console.log( 'server started' );
Without connect middleware
Post objects are in received in chunks,.
using data and end event
var http = require('http');var querystring = require( 'querystring');var util = require('util');var form = require('fs').readFileSync('form.html');var maxData = 2*1024*1024; //prevent mass post datahttp.createServer( function(request, response){if( request.method === 'POST' ){//the post data is received by chunksvar postData = '';request.on('data', function(chunk){postData += chunk;if( postData.length > maxData ){pstData = '';this.pause;//stop further dataresponse.writeHead(413);response.end( 'Request too large');}}).on( 'end', function(){if( !postData ){ response.end(); return; }//prevent empty postvar postDataObject = querystring.parse(postData);//print postData to consoleconsole.log('User Posted: \n', postData );//response with postDataObjectresponse.end( 'You Posted: \n' + util.inspect( postDataObject));});}if( request.method === 'GET' ){response.writeHead(200, {'Content-Type':'text/html'});response.end(form);}}).listen(9000);
Acess POST data using connect module
request.body
var util = require( 'util');var connect = require( 'connect');var form = require( 'fs').readFileSync( 'form.html');//connect.bodyParser allow connect to retrieve POST dataconnect( connect.limit('64kb'), connect.bodyParser(),function( request, response ){if( request.method === 'POST'){console.log( 'User posted:\n', request.body);//request.body is the post objectresponse.end('You Posted:\n'+ util.inspect( request.body));}if( request.method === 'GET'){response.writeHead(200, {'Content-Type': 'text/html'});response.end(form);}}).listen( 9000 );
- Node(6) Accessing GET and POST data using http.ServerRequest
- node.js发送http(get,post)请求
- Get Post Data From Http Request
- Accessing an OpenCV Mat Data using C++
- Fetching data with GET,POST methods by using NSURLConnection
- [API]common http get and post
- erlang http post and get 请求
- HTTP 协议概念 GET and POST 请求
- erlang http post and get 请求【转】
- NodeJS http.ServerRequest & http.ServerResponse
- Http client to POST using multipart/form-data
- php HTTP请求类,支持GET,POST,Multipart/form-data
- Python SOAP API, HTTP POST and HTTP GET
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- Spring Boot 之 Accessing Relational Data using JDBC with Spring
- HTTP Post Using C
- HTTP Post Using C
- Using Data and Variables
- HDU 1671 字典树
- 原生制作纯净64位winpe之一制作
- iOS程序完成后如何生成ipa进行真机测试
- HDU 1848 博弈 SG值
- java初始化小总结
- Node(6) Accessing GET and POST data using http.ServerRequest
- 虚基类声明,初始化及调用顺序
- 外观模式及其改进(一):外观模式概述
- 数据结构小知识点
- Struts1和Struts2的不同【11个方面的比较】
- C#对文件的操作 中文乱码
- android面试题:如何启用Service,如何停用Service?
- 一种在注入进程中使用WTL创建无焦点不在任务栏出现“吸附”窗口的方法和思路
- JTable 为单元格添加按钮效果和事件效果