Nodejs POST data example

24 Feb

Totty explains:

var sys = require('sys'),
http = require('http'),
qs = require('querystring');

http.createServer(function(request, response) {
// data is a string that will be used to join all the chunks
// the POST will be an object with the data from the post
var data = '',
POST;

request.on('data', function(chunk) {
// each time a little chunk (a piece of string) is
// sent to the server the 'data' event is fired
console.log(chunk);
// here is the joining
data += chunk;
});

request.on('end', function(){
// when the 'end' event is fired, then you can
// convert the data to an object with the qs.parse
POST = qs.parse(data);
});

setTimeout(function() {
response.writeHead(200, {'Content-Type': 'application/json',
'Access-Control-Allow-Origin': "*"});
response.write(JSON.stringify(POST));
response.end();
}, 2000);

}).listen(8000);
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: