function Publisher(name) { this.name = name; this.subscribers = []; } Publisher.prototype.deliver = function (data, scope) { scope = scope || window; var self = this; this.subscribers.forEach( function (value) { value.call(scope, self.name, data); } ); return this; }; Function.prototype.subscribe = function (publisher) { var self = this, isExistent = publisher.subscribers.some( function (value) { return (value === self); } ); if (!isExistent) { publisher.subscribers.push(this); } return this; }; Function.prototype.unsubscribe = function (publisher) { var self = this; publisher.subscribers = publisher.subscribers.filter( function(value) { if (value !== self) { return value; } } ); return this; };
var sys = require('sys'), http = require('http'), url = require('url'); http.createServer(function (request, response) { var f = (url.parse(request.url, true)).query.file fs = require('fs'), path = './' + f, rs = fs.createReadStream(path); response.writeHead(200, { 'Content-Type': 'image/png' }); rs.addListener('data', function (b) { response.write(b, 'binary'); }); rs.addListener('end', function () { response.end(); }) }).listen(8000); sys.puts('Server running at http://127.0.0.1:8000/');