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 = '',

request.on('data', function(chunk) {
// each time a little chunk (a piece of string) is
// sent to the server the 'data' event is fired
// 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': "*"});
}, 2000);


Leave a Reply

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

You are commenting using your 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


Get every new post delivered to your Inbox.

%d bloggers like this: