github-view.js
This file
Path
examples/view-constructor/github-view.js
52 lines · 1.0 KiB · JavaScript
Last changed by
examples: fix github view exampleDouglas Christopher Wilsonbefore adoptionb97f6eb
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
/** * Module dependencies. */
var http = require('http') , path = require('path') , extname = path.extname
/** * Expose `GithubView`. */
module.exports = GithubView;
/** * Custom view that fetches and renders * remove github templates. You could * render templates from a database etc. */
function GithubView(name, options){ this.name = name; options = options || {}; this.engine = options.engines[extname(name)]; // "root" is the app.set('views') setting, however // in your own implementation you could ignore this this.path = '/' + options.root + '/master/' + name;}
/** * Render the view. */
GithubView.prototype.render = function(options, fn){ var self = this; var opts = { host: 'raw.githubusercontent.com', port: 443, path: this.path, method: 'GET' };
https.request(opts, function(res) { var buf = ''; res.setEncoding('utf8'); res.on('data', function(str){ buf += str }); res.on('end', function(){ self.engine(buf, options, fn); }); }).end();};