app.js
This file
Path
examples/markdown/app.js
44 lines · 944 bytes · JavaScript
Last changed by
Updated markdown example with acceptance test.Robert Sköldbefore adoption4dbaaa2
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344
/** * Module dependencies. */
var express = require('../../') , fs = require('fs') , md = require('node-markdown').Markdown;
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(e){ fn(e) } })})
// Optional since express defaults to CWD/viewsapp.set('views', __dirname + '/views');app.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' });})
if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}