index.js
This file
Path
examples/view-constructor/index.js
47 lines · 1.1 KiB · JavaScript
Last changed by
Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
History of this fileTake it
add "view" constructor setting to override view behaviourCaridy Patinobefore adoption0b4e2df
lines 1–8why, one step back12/**3 * Module dependencies.4 */56var express = require('../../')7 , http = require('http')8 , GithubView = require('./github-view')Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
line 9why, one step back9 , md = require('marked').parse;add "view" constructor setting to override view behaviourCaridy Patinobefore adoption0b4e2df
lines 10–44why, one step back1011var app = module.exports = express();1213// register .md as an engine in express view system14app.engine('md', function(str, options, fn){15 try {16 var html = md(str);17 html = html.replace(/\{([^}]+)\}/g, function(_, name){18 return options[name] || '';19 })20 fn(null, html);21 } catch(err) {22 fn(err);23 }24})2526// pointing to a particular github repo to load files from it27app.set('views', 'visionmedia/express');2829// register a new view constructor30app.set('view', GithubView);3132app.get('/', function(req, res){33 // rendering a view relative to the repo.34 // app.locals, res.locals, and locals passed35 // work like they normally would36 res.render('examples/markdown/views/index.md', { title: 'Example' });37})3839app.get('/Readme.md', function(req, res){40 // rendering a view from https://github.com/visionmedia/express/blob/master/Readme.md41 res.render('Readme.md');42})4344if (!module.parent) {fix example portTJ Holowaychukbefore adoption579857c
line 45why, one step back45 app.listen(3000);add "view" constructor setting to override view behaviourCaridy Patinobefore adoption0b4e2df
lines 46–47why, one step back46 console.log('Express started on port 3000');47}