Skip to content

Instantly share code, notes, and snippets.

@PaulMougel
Created April 1, 2015 08:08
Show Gist options
  • Save PaulMougel/f9a819442f670d70a1e1 to your computer and use it in GitHub Desktop.
Save PaulMougel/f9a819442f670d70a1e1 to your computer and use it in GitHub Desktop.
bind example
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);
});
$ 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] ...
$ 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