Created
October 15, 2016 16:11
-
-
Save ZhihaoLau/a39f63b97b3dd715dfad45fc524d55d4 to your computer and use it in GitHub Desktop.
configurable middleware example
This file contains hidden or 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
function configableLogger(format) { | |
var re = /:(\w+)/g; | |
return function(req, res, next) { | |
var str = format.replace(re, (match, property) => { | |
return req[property]; | |
}); | |
console.log(str); | |
next(); | |
} | |
} | |
// usage: | |
const connect = require('connect'), | |
app = connect(); | |
app | |
.use(configableLogger(':method :url')) | |
.listen(3000, () => { | |
console.log('running'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment