Skip to content

Instantly share code, notes, and snippets.

@adrianke
Created August 5, 2015 16:17
Show Gist options
  • Save adrianke/24c23e91c6982c3a04ca to your computer and use it in GitHub Desktop.
Save adrianke/24c23e91c6982c3a04ca to your computer and use it in GitHub Desktop.
Form handling
var http = require('http');
var qs = require('querystring');
var fs = require('fs');
http.createServer(function (req, res) {
if (req.method != "POST") {
res.writeHead(400);
res.end();
return;
}
var body = '';
req.on('data', function (data) {
body += data;
if (body.length > 1e6) {
req.connection.destroy();
}
});
req.on('end', function () {
var post = qs.parse(body);
fs.appendFileSync('submissions.txt', JSON.stringify(post));
res.writeHead(302, {Location: post['_redirect']});
res.end();
});
}).listen(4000);
console.log("Listening on port 4000");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment