Skip to content
Myr

github-view.js

This file

Path

examples/view-constructor/github-view.js

52 lines · 1.0 KiB · JavaScript

Last changed by

add "view" constructor setting to override view behaviourCaridy Patinobefore adoption0b4e2df

History of this file

Take it

add "view" constructor setting to override view behaviourCaridy Patinobefore adoption0b4e2df

lines 1–52why, one step back
1
2/**3 * Module dependencies.4 */56var http = require('http')7 , path = require('path')8 , extname = path.extname910/**11 * Expose `GithubView`.12 */1314module.exports = GithubView;1516/**17 * Custom view that fetches and renders18 * remove github templates. You could19 * render templates from a database etc.20 */2122function GithubView(name, options){23 this.name = name;24 options = options || {};25 this.engine = options.engines[extname(name)];26 // "root" is the app.set('views') setting, however27 // in your own implementation you could ignore this28 this.path = '/' + options.root + '/master/' + name;29}3031/**32 * Render the view.33 */3435GithubView.prototype.render = function(options, fn){36 var self = this;37 var opts = {38 host: 'rawgithub.com',39 port: 80,40 path: this.path,41 method: 'GET'42 };4344 http.request(opts, function(res) {45 var buf = '';46 res.setEncoding('utf8');47 res.on('data', function(str){ buf += str });48 res.on('end', function(){49 self.engine(buf, options, fn);50 });51 }).end();52};