Last active
October 26, 2017 11:49
-
-
Save vishva8kumara/a3804a80860262cfe8effe69accdd0de to your computer and use it in GitHub Desktop.
HTTP - Handler for multi-part POST request/response body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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