Created
March 2, 2012 05:05
-
-
Save ag4ve/1955833 to your computer and use it in GitHub Desktop.
Simple blog with node.js, express and mongodb
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
var express = require('express'), | |
app = express.createServer(), | |
Post = require('./models/post'); | |
app.configure(function () { | |
app.use(express.methodOverride()); | |
app.use(express.bodyDecoder()); | |
app.use(express.staticProvider(__dirname + '/public')); | |
app.use(express.compiler({src: __dirname + '/public', enable: ['sass']})); | |
app.use(express.logger()); | |
app.set('view engine', 'haml'); | |
app.register('.haml', require('hamljs')); | |
}); | |
app.get('/', function (req, res) { | |
Post.find().all(function (docs) { | |
res.render('index', { | |
locals: {title: 'Blog', articles: docs} | |
}); | |
}); | |
}); | |
app.get('/blog/new', function(req, res) { | |
res.render('new', {locals: {title: 'New Post'}}); | |
}); | |
app.post('/blog/new', function(req, res) { | |
var p = new Post(); | |
p.title = req.body.title; | |
p.body = req.body.body; | |
p.save(function () { | |
res.redirect('/'); | |
}); | |
}); | |
app.post('/blog/addComment', function(req, res) { | |
Post.findById(req.body._id, function(article) { | |
var comment = {person: req.body.person, comment: req.body.comment, created_at: new Date()}; | |
article.comments.unshift(comment); | |
article.save(function() { | |
res.redirect('/blog/' + req.body._id); | |
}); | |
}); | |
}); | |
app.get('/blog/:id', function(req, res) { | |
Post.findById(req.params.id, function(data) { | |
res.render('view', {locals: {title: data.title, article: data}}); | |
}); | |
}); | |
app.listen(); |
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
%h1= title | |
#articles | |
- each article in articles | |
%div.article | |
%div.created_at= article.created_at | |
%div.title | |
%a{href: "/blog/" + article._id.toHexString()}= article.title | |
%div.body= article.body |
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
!!! 5 | |
%html | |
%head | |
%title= title | |
%link{rel: 'stylesheet', href: '/css/style.css', type: 'text/css'} | |
%body | |
#wrapper | |
!= body |
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
%h1= title | |
%form{method: 'post', action: '/blog/new'} | |
%div | |
%div | |
%span Title : | |
%input{type: 'text', name: 'title', id: 'editArticleTitle'} | |
%div | |
%span Body : | |
%textarea{name: 'body', rows: 20, id: 'editArticleBody'} | |
%div#editArticleSubmit | |
%input{type: 'submit', value: 'Send'} |
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
var mongoose = require('mongoose').Mongoose, | |
db = mongoose.connect('mongodb://localhost/blog'); | |
mongoose.model('Post', { | |
properties: [ | |
'title', 'body', | |
{comments: [['person', 'comment', 'created_at']]}, | |
'created_at' | |
], | |
methods: { | |
save: function (fn) { | |
this.created_at = new Date(); | |
this.__super__(fn); | |
} | |
} | |
}); | |
module.exports = db.model('Post'); |
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
body | |
:font-family "Helvetica Neue", "Lucida Grande", "Arial" | |
:font-size 13px | |
:text-align center | |
=text-stroke 1px rgba(255, 255, 255, 0.1) | |
:color #555 | |
h1, h2 | |
:margin 0 | |
:font-size 22px | |
:color #343434 | |
h1 | |
:text-shadow 1px 2px 2px #DDD | |
:font-size 60px | |
#articles | |
:text-align left | |
:margin-left auto | |
:margin-right auto | |
:width 320px | |
.article | |
:margin 20px | |
.created_at | |
:display none | |
.title | |
:font-weight bold | |
:text-decoration underline | |
:background-color #EEE | |
.body | |
:background-color #FFA | |
#article | |
.created_at | |
:display none | |
input[type=text] | |
:width 490px | |
:margin-left 16px | |
input[type=button] | |
:text-align left | |
:margin-left 440px | |
textarea | |
:width 490px | |
:height 90px |
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
%h1= title | |
%div#article | |
%div.created_at= article.created_at | |
%div.title= article.title | |
%div.body= article.body | |
- each comment in article.comments | |
%div.comment | |
%div.person= comment.person | |
%div.comment= comment.comment | |
%div | |
%form{method: 'post', action: "/blog/addComment"} | |
%input{type: 'hidden', name: '_id', value: article._id.toHexString()} | |
%div | |
%span Author : | |
%input{type: 'text', name: 'person', id: 'addCommentPerson'} | |
%div | |
%span Comment : | |
%textarea{name: 'comment', row: 5, id: 'addCommentComment'} | |
%div#editArticleSubmit | |
%input{type: 'submit', value: 'Send'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment