Created
April 1, 2015 08:08
-
-
Save PaulMougel/f9a819442f670d70a1e1 to your computer and use it in GitHub Desktop.
bind example
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 Logger = function (level) { | |
this.level = level; | |
this.log = function (message) { | |
if (message === undefined) { | |
message = "..."; | |
} | |
console.log('[' + this.level + '] ' + message); | |
}; | |
}; | |
debug_logger = new Logger('debug'); | |
request_logger = new Logger('request'); | |
debug_logger.log('Starting the application'); | |
var app = express(); | |
app.get('/', function (req, res) { | |
request_logger.log('Request received on /') | |
res.send('Hello World!'); | |
// res.on('finish', request_logger.log.bind(request_logger)); | |
res.on('finish', request_logger.log); | |
}); | |
var server = app.listen(3333, function () { | |
var host = server.address().address; | |
var port = server.address().port; | |
debug_logger.log('Application started on port ' + port); | |
}); |
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
$ node index.js | |
[debug] Starting the application | |
[debug] Application started on port 3333 | |
# on another shell, run curl http://localhost:3333/ | |
[request] Request received on / | |
[request] ... |
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
$ node index.js | |
[debug] Starting the application | |
[debug] Application started on port 3333 | |
# on another shell, run curl http://localhost:3333/ | |
[request] Request received on / | |
[undefined] ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment