Skip to content

Instantly share code, notes, and snippets.

@firedfox
Last active April 2, 2018 14:38
Show Gist options
  • Save firedfox/df4e21304ae24d78c83290bbae75dafa to your computer and use it in GitHub Desktop.
Save firedfox/df4e21304ae24d78c83290bbae75dafa to your computer and use it in GitHub Desktop.
const http = require('http');
const PORT = 3000;
const server = http.createServer((req, res) => {
if (req.method !== 'POST') {
return res.end('');
}
console.log('\nheader:');
console.log(JSON.stringify(req.headers));
const buffers = [];
let bufferLength = 0;
req.on('data', data => {
bufferLength += data.length;
buffers.push(data);
});
req.on('end', () => {
console.log("body:");
const body = Buffer.concat(buffers, bufferLength);
if (req.headers['content-type'] === 'x-www-form-urlencoded') {
const params = body.toString().replace(/\s/g, '').split('&').sort();
params.forEach(item => console.log(decodeURIComponent(item).replace(/\s/g, '')));
}
else {
try { console.log(JSON.parse(body.toString())); }
catch (e) { console.log(e, body); }
}
res.end('');
});
});
server.listen(PORT);
console.log('server started. listening on', PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment