Last active
November 16, 2015 19:30
-
-
Save tmathmeyer/12e5176176cab1f89e8d to your computer and use it in GitHub Desktop.
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 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