I am trying to resolve a 'this problem' in a Node server. If I don't replace the following worker.stuff
with worker.stuff.bind(worker)
it doesn't work.
Is there a way to add the bind
in the Worker Class itself? Using it every time I call the function is boring.
Here is the server:
var app = require("express")()
, Worker = require('./worker')
, worker = new Worker();
app.get("/stuff", worker.stuff);
worker.on('done', function () {
console.log('done')
});
app.listen(3000);
Here is the worker:
function Worker(options) {}
require('util').inherits(Worker, require('events').EventEmitter);
Worker.prototype.stuff = function (req, res) {
this.emit('done');
res.send();
}
module.exports = Worker;