Created
June 23, 2016 08:57
-
-
Save dreamingblackcat/ee43130f59262d660a0f80eccca7fa31 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 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