index.js
This file
Path
examples/markdown/index.js
45 lines · 978 bytes · JavaScript
Last changed by
Not known.
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546/** * Module dependencies. */
var express = require('../..');var fs = require('fs');var md = require('marked').parse;
var app = module.exports = express();
// register .md as an engine in express view system
app.engine('md', function(path, options, fn){ fs.readFile(path, 'utf8', function(err, str){ if (err) return fn(err); try { var html = md(str); html = html.replace(/\{([^}]+)\}/g, function(_, name){ return options[name] || ''; }); fn(null, html); } catch(err) { fn(err); } });});
app.set('views', __dirname + '/views');
// make it the default so we dont need .mdapp.set('view engine', 'md');
app.get('/', function(req, res){ res.render('index', { title: 'Markdown Example' });});
app.get('/fail', function(req, res){ res.render('missing', { title: 'Markdown Example' });});
/* istanbul ignore next */if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}