Created
April 29, 2012 03:47
-
-
Save deoxxa/2531422 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
var express = require('express'), | |
db = require('mongojs').connect('mongodb://', ['db']), | |
routes = require('./routes')(db), | |
app = module.exports = express.createServer(); | |
app.configure(function() { | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.configure('development', function() { | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function() { | |
app.use(express.errorHandler()); | |
}); | |
app.get('/', routes.index); | |
app.get('/:page([0-9]{1,3})?', routes.index) | |
app.get('/:slug([a-z0-9-]{3,255})', routes.item); | |
app.listen(8080); | |
app.on('listening', function() { | |
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); | |
}); |
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
module.exports = function(db) { | |
var obj = {}; | |
obj.index = function(req, res) { | |
if (req.params.page) { | |
res.send('Viewing page: ' + req.params.page) | |
} | |
else | |
{ | |
res.send(db) | |
} | |
}; | |
obj.item = function(req, res) { | |
db.blog_posts.find({slug: req.param('slug')}, function (err, post) { | |
if (err || !post.length) { | |
res.send('Could not find a post with the slug: ' + req.param('slug')); | |
} | |
else | |
{ | |
res.send(post.title); | |
} | |
}); | |
}; | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment