Skip to content

Instantly share code, notes, and snippets.

@dreamingblackcat
Created June 23, 2016 08:57
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 dreamingblackcat/ee43130f59262d660a0f80eccca7fa31 to your computer and use it in GitHub Desktop.
Save dreamingblackcat/ee43130f59262d660a0f80eccca7fa31 to your computer and use it in GitHub Desktop.
var express = require('express')
var responseTime = require('response-time');
var app = express();
var monkeyPatch = function(req, res, next) {
// only monkey patched json method , you get the idea
var oldJson = res.json;
res.json = function(obj) {
res.resData = obj;
console.log("monkey patched json method!");
oldJson.apply(this, arguments);
}
next();
}
app.use(monkeyPatch);
app.use( responseTime(function(req, res, time) {
var log = {
"date" : new Date(),
"os" : req.headers['user-agent'],
"requestUrl" : req.originalUrl,
"ipAddress" : req.ip,
"requestMethod" : req.method,
"statusCode" : res.statusCode,
"statusMessage" : "",
"timeTaken" : Math.floor(time),
"data" : JSON.stringify(req.body)
};
// response is in res.resData
console.log(log, res.resData);
}))
app.get('/', function (req, res) {
res.send('hello, world!')
})
app.get('/error', function (req, res) {
res.status(400).json({
error: "it's error"
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment