Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring, comments, cleanup

  • Loading branch information...
commit a2f33c3d2364ba7d7453a6b9a7f968eb3e82e8c5 1 parent 8d3bb05
@raymondfeng raymondfeng authored
View
1  .gitignore
@@ -1,3 +1,4 @@
.DS_Store
node_modules
+.project
View
4 app.js
@@ -12,7 +12,7 @@ var express = require('express')
, http = require('http')
, path = require('path')
, config = require('./config/config.js')
- , mongo = require('./mongo-store.js')
+ , mongo = require('./db/mongo-store.js')
, setup = require('./app-setup.js');
@@ -69,5 +69,5 @@ resource.setup(app, mongo.mongoose);
// Start to listen on the HTTP port
http.createServer(app).listen(app.get('port'), function(){
- console.log("Express server listening on port " + app.get('port'));
+ console.log("Sample blog server listening on port " + app.get('port'));
});
View
2  mongo-store.js → db/mongo-store.js
@@ -2,7 +2,7 @@
* A module to connect to a MongoDB store
*/
var mongoose = require('mongoose'),
- config = require('./config/config.js')
+ config = require('../config/config.js')
exports.mongoose = mongoose;
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
View
3  models/blog.js
@@ -1,3 +1,6 @@
+/**
+ * Blog schema
+ */
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
View
3  models/user.js
@@ -1,3 +1,6 @@
+/**
+ * User schema
+ */
var crypto = require('crypto'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
View
53 routes/auth.js
@@ -13,22 +13,24 @@ var passport = require('passport')
* Anytime a request is made to authorize an application, we must ensure that
* a user is logged in before asking them to approve the request.
*/
-passport.use(new LocalStrategy(
- function(username, password, done) {
- User.findByUsernamePassword(username, password, function(err, user) {
- if (err) { return done(err); }
- if (!user) { return done(null, false); }
- return done(null, user);
- });
- }
-));
+passport.use(new LocalStrategy(function(username, password, done) {
+ User.findByUsernamePassword(username, password, function(err, user) {
+ if (err) {
+ return done(err);
+ }
+ if (!user) {
+ return done(null, false);
+ }
+ return done(null, user);
+ });
+}));
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
- User.findById(id, function (err, user) {
+ User.findById(id, function(err, user) {
done(err, user);
});
});
@@ -37,30 +39,35 @@ exports.loginForm = function(req, res) {
res.render('login');
};
-exports.login = passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' });
+exports.login = passport.authenticate('local', {
+ successReturnToOrRedirect : '/',
+ failureRedirect : '/login'
+});
exports.logout = function(req, res) {
req.logout();
res.redirect('/');
}
-exports.account = [
- ensure.ensureLoggedIn('/login'),
- function(req, res) {
- res.render('account', { user: req.user });
- }
-]
+exports.account = [ ensure.ensureLoggedIn('/login'), function(req, res) {
+ res.render('account', {
+ user : req.user
+ });
+} ]
+/**
+ * Set up the login handler
+ */
exports.setup = function(app) {
app.use(passport.initialize());
app.use(passport.session());
app.all('/post', function(req, res, next) {
- console.log(req.path);
- if(req.path === "/login") {
- next();
- } else {
- ensure.ensureLoggedIn('/login')(req, res, next);
- }
+ console.log(req.path);
+ if (req.path === "/login") {
+ next();
+ } else {
+ ensure.ensureLoggedIn('/login')(req, res, next);
+ }
});
app.get('/login', exports.loginForm);
app.post('/login', exports.login);
View
79 routes/index.js
@@ -2,48 +2,63 @@
* GET home page.
*/
- var ejs = require('ejs')
- , config = require('../config/config.js')
- , Blog = require('../models/blog');
+var ejs = require('ejs')
+ , config = require('../config/config.js')
+ , Blog = require('../models/blog');
-exports.index = function (req, res) {
- Blog.find({}).sort({'date': 'descending'}).execFind( function (err, blogs) {
- // Disable cache so that the page refreshes
- res.setHeader('Cache-Control', 'no-store');
- res.render('index', {
- title: 'Sample Blog Application',
- blogs: blogs});
- });
+/**
+ * Render the home page with a list of blogs
+ */
+exports.index = function(req, res) {
+ Blog.find({}).sort({
+ 'date' : 'descending'
+ }).execFind(function(err, blogs) {
+ // Disable cache so that the page refreshes
+ res.setHeader('Cache-Control', 'no-store');
+ res.render('index', {
+ title : 'Sample Blog Application',
+ blogs : blogs
+ });
+ });
};
-
+/**
+ * Add a new comment
+ */
exports.postComment = function(req, res) {
- var comment = {
- author: config.demo.user,
- body: req.body.body
- };
+ var comment = {
+ author : config.demo.user,
+ body : req.body.body
+ };
- Blog.update({_id: req.body.postId}, {$push: { 'comments' : comment }} , function(err, blog) {
- if(err) {
- console.err(err);
- } else {
- console.log(blog);
- }
- });
+ Blog.update({
+ _id : req.body.postId
+ }, {
+ $push : {
+ 'comments' : comment
+ }
+ }, function(err, blog) {
+ if (err) {
+ console.err(err);
+ } else {
+ console.log(blog);
+ }
+ });
- res.redirect("/");
+ res.redirect("/");
}
-
+/**
+ * Set up the routes
+ */
exports.setup = function(app) {
- app.get('/', exports.index);
- app.post('/postComment', exports.postComment);
+ app.get('/', exports.index);
+ app.post('/postComment', exports.postComment);
};
-/*
+/**
* Filter to handle dates.
*/
-
- ejs.filters.displayDate = function(date) {
- return date;
- }
+ejs.filters.displayDate = function(date) {
+ return date;
+}
View
43 routes/post.js
@@ -1,25 +1,40 @@
+/**
+ * Handling Blog posting
+ */
var config = require('../config/config.js')
- , Blog = require('../models/blog');
-
+ , Blog = require('../models/blog');
+/**
+ * Render the Blog posting form
+ */
exports.post = function(req, res) {
- res.render('post', {
- title: 'Sample Blog Application - New Blog post',
- author: req.user.username
- });
+ res.render('post', {
+ title : 'Sample Blog Application - New Blog post',
+ author : req.user.username
+ });
}
+/**
+ * Save the blog entry and redirect to / for refreshing
+ */
exports.save = function(req, res) {
- Blog.create({title: req.body.title, body: req.body.body, author: req.user.username}, function(err, blog) {
- if(err) {
- console.err(err);
- } else {
- console.log(blog);
- }
- });
- res.redirect("/");
+ Blog.create({
+ title : req.body.title,
+ body : req.body.body,
+ author : req.user.username
+ }, function(err, blog) {
+ if (err) {
+ console.err(err);
+ } else {
+ console.log(blog);
+ }
+ });
+ res.redirect("/");
}
+/**
+ * Set up the Express routes
+ */
exports.setup = function(app) {
app.get('/post', exports.post);
app.post('/post', exports.save);
View
36 routes/resource.js
@@ -1,6 +1,12 @@
+/**
+ * REST APIs for mongoose models that supports CRUD operations
+ */
var User = require('../models/user')
, Blog = require('../models/blog');
+/**
+ * Create a new entity
+ */
exports.create = function(mongoose) {
var mongo = mongoose;
return function(req, res, next) {
@@ -21,8 +27,8 @@ exports.create = function(mongoose) {
};
}
-/*
- * GET resource listing.
+/**
+ * List all entities
*/
exports.list = function(mongoose) {
var mongo = mongoose;
@@ -55,6 +61,9 @@ exports.list = function(mongoose) {
};
}
+/**
+ * Find an entity by id
+ */
exports.findById = function(mongoose) {
var mongo = mongoose;
return function(req, res, next) {
@@ -76,6 +85,9 @@ exports.findById = function(mongoose) {
};
}
+/**
+ * Delete an entity by id
+ */
exports.deleteById = function(mongoose) {
var mongo = mongoose;
return function(req, res, next) {
@@ -97,6 +109,9 @@ exports.deleteById = function(mongoose) {
};
}
+/**
+ * Update an entity by id
+ */
exports.updateById = function(mongoose) {
var mongo = mongoose;
return function(req, res, next) {
@@ -121,19 +136,24 @@ exports.updateById = function(mongoose) {
/**
* Expose the CRUD operations as REST APIs
*/
-exports.setup = function(app, mongoose) {
+exports.setup = function(app, options) {
+ options = options || {};
+ mongoose = options.mongoose || require('../db/mongo-store').mongoose;
+
+ var base = options.path || '/rest';
+
// Create a new entity
- app.post('/rest/:resource', exports.create(mongoose));
+ app.post(base + '/:resource', exports.create(mongoose));
// List the entities
- app.get('/rest/:resource', exports.list(mongoose));
+ app.get(base + '/:resource', exports.list(mongoose));
// Find the entity by id
- app.get('/rest/:resource/:id', exports.findById(mongoose));
+ app.get(base + '/:resource/:id', exports.findById(mongoose));
// Update the entity by id
- app.put('/rest/:resource/:id', exports.updateById(mongoose));
+ app.put(base + '/:resource/:id', exports.updateById(mongoose));
// Delete the entity by id
- app.delete('/rest/:resource/:id', exports.deleteById(mongoose));
+ app.delete(base + '/:resource/:id', exports.deleteById(mongoose));
}
Please sign in to comment.
Something went wrong with that request. Please try again.