Skip to content

Instantly share code, notes, and snippets.

@tmathmeyer
Last active November 16, 2015 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmathmeyer/12e5176176cab1f89e8d to your computer and use it in GitHub Desktop.
Save tmathmeyer/12e5176176cab1f89e8d to your computer and use it in GitHub Desktop.
var qs = require("querystring");
var http = require("http");
http.createServer(function(request, response) {
if (request.method === 'POST' || request.method === 'post') {
withPostData(request, function(error, postData) {
// postData is a JSON object from the http request
console.log(error); // if there are no problems, error is just null
console.log(postData); // if there are errors, postData is null
// do real things with post data
});
} else {
// otherwise it is *most likely* a get request, don't need data
// do other things
}
}).listen(8080);
withPostData = function(request, callback) {
var body = ''; // initialize a blank body
request.on('data', function(data) {
body += data; // append to data
if (body.length > 1e6) {
request.connection.destroy();
callback({error: "filesize too large: > 1GB"}, null);
}
});
request.on('end', function() {
callback(null, qs.parse(body)); // done reading data, parse it now.
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment