github-view.js
This file
Path
examples/view-constructor/github-view.js
53 lines · 1.0 KiB · JavaScript
Last changed by
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354'use strict'
/** * Module dependencies. */
var https = require('https');var path = require('path');var 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();};