Node(6) Accessing GET and POST data using http.ServerRequest
2012-07-29 00:08
344 查看
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 example:
using data and end event
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 url var parts = url.parse( request.url ) //write the GET details to response response.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 data http.createServer( function(request, response){ if( request.method === 'POST' ){ //the post data is received by chunks var postData = ''; request.on('data', function(chunk){ postData += chunk; if( postData.length > maxData ){ pstData = ''; this.pause;//stop further data response.writeHead(413); response.end( 'Request too large'); } }).on( 'end', function(){ if( !postData ){ response.end(); return; }//prevent empty post var postDataObject = querystring.parse(postData); //print postData to console console.log('User Posted: \n', postData ); //response with postDataObject response.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.bodyvar util = require( 'util'); var connect = require( 'connect'); var form = require( 'fs').readFileSync( 'form.html'); //connect.bodyParser allow connect to retrieve POST data connect( connect.limit('64kb'), connect.bodyParser(), function( request, response ){ if( request.method === 'POST'){ console.log( 'User posted:\n', request.body); //request.body is the post object response.end('You Posted:\n'+ util.inspect( request.body)); } if( request.method === 'GET'){ response.writeHead(200, { 'Content-Type': 'text/html' }); response.end(form); } }).listen( 9000 );
相关文章推荐
- Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
- Get data from specified URI using WebRequest and WebResponse(读取网页数据并存入对应html文档)
- 几个http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- Get Post Data From Http Request
- How to use HttpWebRequest to post data to another page which is on another server
- How to get blob data using javascript XmlHttpRequest by sync
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- C# http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- POST a string and an image in one request using HttpClient
- HttpClient Post Form data and get Response String
- using HttpClient and sending json data to RESTful server in adroind
- Get、Post(HTTP中最常用的请求方式 使用ASIHTTPRequest)----例如:登录、上传图片(调用手机相册 UIImage 转为data)
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- HttpWebRequest GET POST 应用