Last active
August 3, 2016 06:09
-
-
Save drhanlau/b1b0fb4b21e0b5b0dd76696458b34f93 to your computer and use it in GitHub Desktop.
Blog system boilerplate
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 bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var app = express(); | |
mongoose.connect('mongodb://localhost/blog_system'); | |
var db = mongoose.connection; | |
db.on('error', function (err) { | |
console.log('Connection error: ', err); | |
}); | |
db.once('open', function (callback) { | |
console.log('Successfully connected to 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'); | |
var path = require('path'); | |
var favicon = require('serve-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var session = require('express-session'); | |
var flash = require('express-flash'); | |
var methodOverride = require('method-override'); | |
var app = express(); | |
mongoose.connect('mongodb://localhost/blog_system'); | |
var db = mongoose.connection; | |
db.on('error', function (err) { | |
console.log('Connection error: ', err); | |
}); | |
db.once('open', function (callback) { | |
console.log('Successfully connected to MongoDB'); | |
}); | |
// view engine setup | |
app.set('views', path.join(__dirname, '../app/views')); | |
app.set('view engine', 'ejs'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(path.join(__dirname, '../public', 'favicon.ico'))); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(cookieParser()); | |
app.use(session({ | |
resave: true, | |
saveUninitialized: true, | |
secret: '1w2s1d3g43', | |
cookie: { maxAge: 60000 } | |
})); | |
app.use(flash()); | |
app.use(methodOverride('_method')); | |
app.use(express.static(path.join(__dirname, '../public'))); | |
app.use('/', require('./routes')); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
module.exports = app; |
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
exports._index = function(req, res, next){ | |
res.send('GET: /posts/:post_id/comments'); | |
}; | |
exports._new = function(req, res, next){ | |
res.send('GET: /posts/:post_id/comments/new'); | |
}; | |
exports.create = function(req, res, next){ | |
res.send('POST: /posts/:post_id/comments'); | |
}; | |
exports.show = function(req, res, next){ | |
res.send('GET: /posts/:post_id/comments/:comment_id'); | |
}; | |
exports.edit = function(req, res, next){ | |
res.send('GET: /posts/:post_id/comments/:comment_id/edit'); | |
}; | |
exports.update = function(req, res, next){ | |
res.send('PUT: /posts/:post_id/comments/:comment_id'); | |
}; | |
exports.destroy = function(req, res, next){ | |
res.send('DELETE: /posts/:post_id/comments/:comment_id'); | |
}; |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<h1>Editing Post</h1> | |
<form action="/posts/<%= post._id %>?_method=PUT" method="post"> | |
<div class="field"> | |
<label>Title</label><br> | |
<input type="text" name="post[title]" value="<%= post.title %>"> | |
</div> | |
<div class="field"> | |
<label>Author</label><br> | |
<input type="text" name="post[author]" value="<%= post.author %>"> | |
</div> | |
<div class="actions"> | |
<input type="submit" name="commit" value="Update Post"> | |
</div> | |
</form> | |
<a href="/posts/<%= post._id %>">Show</a> | | |
<a href="/posts">Back</a> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Oops!</title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<h1>Oops!</h1> | |
<p>There's an error. <br>Please contact the administrator for more info: <br><%= err %></p> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<% if (messages.notice) { %> | |
<p id="notice"><%= messages.notice %></p> | |
<% } %> | |
<h1>Listing Posts</h1> | |
<table> | |
<thead> | |
<tr> | |
<th>Title</th> | |
<th>Author</th> | |
<th>Date</th> | |
<th colspan="3"></th> | |
</tr> | |
</thead> | |
<tbody> | |
<% posts.forEach(function(post) { %> | |
<tr> | |
<td><%= post.title %></td> | |
<td><%= post.author %></td> | |
<td><%= post.date %></td> | |
<td><a href="/posts/<%= post._id %>">Show</a></td> | |
<td><a href="/posts/<%= post._id %>/edit">Edit</a></td> | |
<td><form method="post" action="/posts/<%= post._id %>?_method=delete"><input type="submit" value="Destroy"></form></td> | |
</tr> | |
<% }); %> | |
</tbody> | |
</table> | |
<br> | |
<a href="/posts/new">New Post</a> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<h1>New Post</h1> | |
<form action="/posts" method="post"> | |
<div class="field"> | |
<label>Title</label><br> | |
<input type="text" name="post[title]" value="<%= post.title %>"> | |
</div> | |
<div class="field"> | |
<label>Author</label><br> | |
<input type="text" name="post[author]" value="<%= post.author %>"> | |
</div> | |
<div class="actions"> | |
<input type="submit" name="commit" value="Create Post"> | |
</div> | |
</form> | |
<a href="/posts">Back</a> | |
</body> | |
</html> |
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'); | |
var Schema = mongoose.Schema; | |
var CommentSchema = new Schema({ | |
body: String, | |
email: String, | |
author: String | |
}); | |
var PostSchema = new Schema({ | |
author: String, | |
title: String, | |
permalink: String, | |
body: String, | |
comments: [CommentSchema], | |
tags: [String], | |
date: { type: Date, default: Date.now } | |
}); | |
module.exports = mongoose.model('Post', PostSchema); |
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
exports._index = function(req, res, next){ | |
res.send('GET: /posts'); | |
}; | |
exports._new = function(req, res, next){ | |
res.send('GET: /posts/new'); | |
}; | |
exports.create = function(req, res, next){ | |
res.send('POST: /posts'); | |
}; | |
exports.show = function(req, res, next){ | |
res.send('GET: /posts/:post_id'); | |
}; | |
exports.edit = function(req, res, next){ | |
res.send('GET: /posts/:post_id/edit'); | |
}; | |
exports.update = function(req, res, next){ | |
res.send('PUT: /posts/:post_id'); | |
}; | |
exports.destroy = function(req, res, next){ | |
res.send('DELETE: /posts/:post_id'); | |
}; |
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 Post = require('../models/post'); | |
exports._index = function(req, res, next){ | |
// call the find function | |
Post.find(function(err, posts) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
res.render('posts/index', { posts: posts, title: 'View all posts' }); | |
} | |
}); | |
}; | |
exports._new = function(req, res, next){ | |
res.render('posts/new', { post: new Post({}), title: 'New Post' } ); | |
}; | |
exports.create = function(req, res, next){ | |
var post = new Post(req.body.post); | |
post.save(function(err) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
// so that the notice message will be re-displayed upon a redirect | |
req.flash('notice', 'Post was successfully created.'); | |
res.redirect('/posts/' + post._id); | |
} | |
}); | |
}; | |
exports.show = function(req, res, next){ | |
Post.findOne({ _id: req.params.post_id }, function(err, post) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
res.render('posts/show', { post: post, title: post.title }); | |
} | |
}); | |
}; | |
exports.edit = function(req, res, next){ | |
Post.findOne({ _id: req.params.post_id }, function(err, post) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
res.render('posts/edit', { post: post, title: 'Edit Post' } ); | |
} | |
}); | |
}; | |
exports.update = function(req, res, next){ | |
Post.findOne({ _id: req.params.post_id }, function(err, post) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
for (prop in req.body.post) { | |
post[prop] = req.body.post[prop]; | |
} | |
post.save(function(err) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
// so that the notice message will be re-displayed upon a redirect | |
req.flash('notice', 'Post was successfully updated.'); | |
res.redirect('/posts/' + post._id); | |
} | |
}); | |
} | |
}); | |
}; | |
exports.destroy = function(req, res, next){ | |
Post.remove({ _id: req.params.post_id }, function(err, post) { | |
if (err) { | |
res.render('common/error', { err: err }); | |
} else { | |
// so that the notice message will be re-displayed upon a redirect | |
req.flash('notice', 'Post was successfully destroyed.'); | |
res.redirect('/posts'); | |
} | |
}); | |
}; |
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'); | |
var router = express.Router(); | |
var indexController = require('../app/controllers/indexController'); | |
var usersController = require('../app/controllers/usersController'); | |
var postsController = require('../app/controllers/postsController'); | |
var commentsController = require('../app/controllers/commentsController'); | |
router.get('/', indexController.show); | |
router.get('/users', usersController.show); | |
router.get('/posts', postsController._index); | |
router.get('/posts/new', postsController._new); | |
router.post('/posts', postsController.create); | |
router.get('/posts/:post_id', postsController.show); | |
router.get('/posts/:post_id/edit', postsController.edit); | |
router.put('/posts/:post_id', postsController.update); | |
router.delete('/posts/:post_id', postsController.destroy); | |
router.get('/posts/:post_id/comments', commentsController._index); | |
router.get('/posts/:post_id/comments/new', commentsController._new); | |
router.post('/posts/:post_id/comments', commentsController.create); | |
router.get('/posts/:post_id/comments/:comment_id', commentsController.show); | |
router.get('/posts/:post_id/comments/:comment_id/edit', commentsController.edit); | |
router.put('/posts/:post_id/comments/:comment_id', commentsController.update); | |
router.delete('/posts/:post_id/comments/:comment_id', commentsController.destroy); | |
module.exports = router; |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<% if (messages.notice) { %> | |
<p id="notice"><%= messages.notice %></p> | |
<% } %> | |
<p> | |
<strong>Title:</strong> | |
<%= post.title %> | |
</p> | |
<p> | |
<strong>Author:</strong> | |
<%= post.author %> | |
</p> | |
<a href="/posts/<%= post._id %>/edit">Edit</a> | | |
<a href="/posts">Back</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment