Skip to content

Instantly share code, notes, and snippets.

@andresilva
Last active February 23, 2016 18:46
Show Gist options
  • Select an option

  • Save andresilva/ca776e3a2a0e4c82ceb2 to your computer and use it in GitHub Desktop.

Select an option

Save andresilva/ca776e3a2a0e4c82ceb2 to your computer and use it in GitHub Desktop.
HTTP dev logging server
var finalhandler = require('finalhandler');
var http = require('http');
var morgan = require('morgan');
morgan.token('type', function (req, res) {
return req.headers['content-type'];
});
morgan.token('data', function (req, res) {
return req.data;
});
var logger = morgan('[:date[clf]] - :remote-addr - HTTP/:http-version :method :url (:type) - :data');
http.createServer(function (req, res) {
req.on('data', function (chunk) {
req.data = req.data || '';
req.data += chunk;
});
req.on('end', function () {
logger(req, res, function (err) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('👍 ');
});
});
}).listen(8000, "127.0.0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment