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
The walk stopped after 200 commits: lines older than 454c4b2 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 1–2why, one step back1'use strict'2Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 3–6why, one step back3/**4 * Module dependencies.5 */6Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 7why, one step back7var https = require('https');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 8–38why, one step back8var path = require('path');9var extname = path.extname;1011/**12 * Expose `GithubView`.13 */1415module.exports = GithubView;1617/**18 * Custom view that fetches and renders19 * remove github templates. You could20 * render templates from a database etc.21 */2223function GithubView(name, options){24 this.name = name;25 options = options || {};26 this.engine = options.engines[extname(name)];27 // "root" is the app.set('views') setting, however28 // in your own implementation you could ignore this29 this.path = '/' + options.root + '/master/' + name;30}3132/**33 * Render the view.34 */3536GithubView.prototype.render = function(options, fn){37 var self = this;38 var opts = {Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534
lines 39–40why, one step back39 host: 'raw.githubusercontent.com',40 port: 443,Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 41–44why, one step back41 path: this.path,42 method: 'GET'43 };44Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534
line 45why, one step back45 https.request(opts, function(res) {Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 46–53why, one step back46 var buf = '';47 res.setEncoding('utf8');48 res.on('data', function(str){ buf += str });49 res.on('end', function(){50 self.engine(buf, options, fn);51 });52 }).end();53};