Created
October 3, 2011 10:39
-
-
Save toabi/1258874 to your computer and use it in GitHub Desktop.
Express Example (without view templates)
This file contains hidden or 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
_ = require 'underscore' | |
express = require 'express' | |
db = require './data' | |
app = express.createServer() | |
app.configure -> | |
app.set 'views', __dirname + '/views' | |
app.set 'view engine', 'jade' | |
app.set 'name', 'MiniCMS' | |
app.use express.bodyParser() | |
app.use express.methodOverride() | |
app.use express.static(__dirname + '/public') | |
app.use app.router; | |
app.configure 'development', -> | |
app.use express.errorHandler { dumpExceptions: true, showStack: true } | |
app.configure 'production', -> | |
app.use express.errorHandler() | |
# Middleware | |
loadCategory = (req, res, next) -> | |
if db[req.params.category] | |
req.category = db[req.params.category] | |
next() | |
else | |
throw Error "Category #{req.params.category} does not exist!" | |
# Routes | |
app.get '/:category/', loadCategory, (req, res) -> | |
res.render 'category', { | |
title: app.settings.name + ' | ' + req.params.category | |
categoryName: req.params.category | |
items: req.category | |
} | |
app.get '/:category/:id', loadCategory, (req, res) -> | |
item = req.category[req.params.id] | |
throw Error "Item with id #{req.params.id} does not exist!" if not item | |
res.render 'details', { | |
title: app.settings.name + ' | ' + item.name | |
item: item | |
} | |
app.get '/', (req, res) -> | |
res.render 'index', { | |
title: app.settings.name | |
categories: _.keys db | |
} | |
app.listen 3000; | |
console.log "Express server listening on port %d in %s mode", app.address().port, app.settings.env |
This file contains hidden or 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
module.exports = | |
vegetables: | |
0: | |
name: 'Apple' | |
price: '1€' | |
desc: 'An apple a day keeps the doctor away.' | |
1: | |
name: 'Onion' | |
price: '0.3€' | |
desc: 'You will burst into tears!' | |
2: | |
name: 'Lettuce' | |
price: '1.25€' | |
desc: 'I am a a cultivated plant of the daisy family, with edible | |
leaves that are a usual ingredient of salads.' | |
meat: | |
0: | |
name: 'Pork', | |
price: '9€', | |
desc: 'Oink oink!', | |
1: | |
name: 'Mouse', | |
price: '1€', | |
desc: 'I am a mouse!', |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment