Created
August 2, 2012 22:29
-
-
Save aackerman/3241400 to your computer and use it in GitHub Desktop.
Example Express Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
path = require('path'), | |
hogan = require('hogan.js'); | |
var dir = 'templates', | |
partials_dir = dir + '/partials', | |
extension = '.hogan', | |
partials = {}; | |
//cache partials | |
var files = fs.readdirSync(partials_dir); | |
files.forEach(function(file){ | |
var partial = fs.readFileSync(path.join(partials_dir, file), 'UTF-8'); | |
partials[path.basename(file, extension)] = hogan.compile(partial); | |
}); | |
var layout = fs.readFileSync(dir + '/layout.hogan', 'utf8'); | |
module.exports = function(filepath, options, fn) { | |
var view = hogan.compile(fs.readFileSync(filepath, 'utf8')); | |
if(layout) { | |
options.body = view.render(options, partials); | |
view = hogan.compile(layout); | |
} | |
fn(null, view.render(options, partials)); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import modules | |
var express = require('express'), | |
adapter = require('./adapter'); | |
//create server | |
var app = express.createServer(); | |
//setup express middleware | |
app.configure(function(){ | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser('a')); | |
app.use(express.session()); | |
app.use(express.static(__dirname + '/public')); | |
app.set('views', __dirname + '/templates'); | |
app.set('view engine', 'hogan'); | |
app.engine('hogan', adapter); | |
}); | |
//import routes | |
require('./routes')(app); | |
//explode server | |
app.listen(7700); | |
console.log('server running on port 7700'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment