Skip to content

Instantly share code, notes, and snippets.

@vishva8kumara
Last active October 26, 2017 11:49
Show Gist options
  • Save vishva8kumara/a3804a80860262cfe8effe69accdd0de to your computer and use it in GitHub Desktop.
Save vishva8kumara/a3804a80860262cfe8effe69accdd0de to your computer and use it in GitHub Desktop.
HTTP - Handler for multi-part POST request/response body
var fs = require('fs');
function handlePost(req, callback){
var body = '';
// Receive multiple chunks
req.on('data', function (data){
body += data;
});
// All chunks received
req.on('end', function (data){
var post = body;
// Try parse as JSON
try{
post = JSON.parse(post);
}
catch(e){
// Try parse as submitted form
try{
post = qs.parse(post);
}
catch(e){}
}
callback(post);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment