Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created June 10, 2013 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfjedimaster/5746567 to your computer and use it in GitHub Desktop.
Save cfjedimaster/5746567 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var ArticleProvider = require('./articleprovider-mongo').ArticleProvider;
var articleProvider = new ArticleProvider('localhost',27017);
var hbs = require('hbs');
var hbHelpers = require('./hbHelpers');
hbs.registerHelper('dateFormat',function() { return "foo"; });
app.configure(function() {
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use("/public",express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret:'foo'}));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.locals({
title:"JavaScript Cookbook"
});
app.get('/', function(req,res) {
articleProvider.findAll(function(err, data) {
res.render('index',{articles:data});
});
});
//Todo: Secure
app.get('/article/new', function(req, res) {
res.render('articlenew', { title: "New Article" });
});
app.post('/article/new', function(req, res) {
articleProvider.save({
title:req.param('title'),
body:req.param('body'),
tags:req.param('tags')
}, function(err, docs) {
res.redirect('/');
});
});
app.get('/article/:id', function(req, res) {
articleProvider.findById(req.params.id, function(error, article) {
res.render('article', {article:article, title:article.title});
});
});
app.get('/tags', function(req, res) {
articleProvider.getTags(function(error, tags) {
res.send(JSON.stringify(tags));
});
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment