Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d6ab228
Showing
56 changed files
with
2,558 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
.monitor | ||
Makefile |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
before_script: | ||
- cp config/config.example.js config/config.js | ||
- cp config/imager.example.js config/imager.js | ||
services: | ||
- mongodb | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
- "0.8" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: ./node_modules/.bin/forever -m 5 server.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
[![Build Status](https://travis-ci.org/madhums/nodejs-express-mongoose-demo.png)](https://travis-ci.org/madhums/nodejs-express-mongoose-demo) | ||
|
||
# Nodejs Express Mongoose Demo | ||
|
||
**Blog post 1:** [Breaking down app.js file - nodejs, express, mongoose](http://madhums.me/2012/07/19/breaking-down-app-js-file-nodejs-express-mongoose/) | ||
|
||
**Blog post 2:** Coming soon! | ||
|
||
This is a demo node.js application illustrating various features used in everyday web development, with a fine touch of best practices. The demo app is a blog application where users (signing up using facebook, twitter, github and simple registrations) can create an article, delete an article and add comments on the article. | ||
|
||
## Install | ||
|
||
**NOTE:** You need to have node.js, mongodb and [imagemagick](http://www.imagemagick.org/script/index.php) installed and running. | ||
|
||
```sh | ||
$ git clone git://github.com/madhums/nodejs-express-mongoose-demo.git | ||
$ npm install | ||
$ cp config/config.example.js config/config.js | ||
$ cp config/imager.example.js config/imager.js | ||
$ npm start | ||
``` | ||
|
||
**NOTE:** Do not forget to update your facebook twitter and github APP_ID and APP_SECRET in `config/config.js`. Also if you want to use image uploads, don't forget to replace the S3 and Rackspace keys in `config/imager.js`. | ||
|
||
Then visit [http://localhost:3000/](http://localhost:3000/) | ||
|
||
## Info | ||
|
||
1. [node-genem](https://github.com/madhums/node-genem) A module to generate the MVC skeleton using this approach. | ||
2. [node-notifier](http://github.com/madhums/node-notifier) - used for notifications via emails and push notificatiions | ||
3. [node-imager](http://github.com/madhums/node-imager) - used to resize, crop and upload images to S3/rackspace | ||
4. [node-view-helpers](http://github.com/madhums/node-view-helpers) - some common view helpers | ||
|
||
## Directory structure | ||
``` | ||
-app/ | ||
|__controllers/ | ||
|__models/ | ||
|__mailer/ | ||
|__views/ | ||
-config/ | ||
|__routes.js | ||
|__config.js | ||
|__passport.js (auth config) | ||
|__imager.js (imager config) | ||
|__express.js (express.js configs) | ||
|__middlewares/ (custom middlewares) | ||
-public/ | ||
``` | ||
|
||
## Tests | ||
|
||
```sh | ||
$ npm test | ||
``` | ||
|
||
## License | ||
(The MIT License) | ||
|
||
Copyright (c) 2012 Madhusudhan Srinivasa < [madhums8@gmail.com](mailto:madhums8@gmail.com) > | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var mongoose = require('mongoose') | ||
, Imager = require('imager') | ||
, async = require('async') | ||
, Article = mongoose.model('Article') | ||
, _ = require('underscore') | ||
|
||
/** | ||
* Find article by id | ||
*/ | ||
|
||
exports.article = function(req, res, next, id){ | ||
var User = mongoose.model('User') | ||
|
||
Article.load(id, function (err, article) { | ||
if (err) return next(err) | ||
if (!article) return next(new Error('Failed to load article ' + id)) | ||
req.article = article | ||
next() | ||
}) | ||
} | ||
|
||
/** | ||
* New article | ||
*/ | ||
|
||
exports.new = function(req, res){ | ||
res.render('articles/new', { | ||
title: 'New Article', | ||
article: new Article({}) | ||
}) | ||
} | ||
|
||
/** | ||
* Create an article | ||
*/ | ||
|
||
exports.create = function (req, res) { | ||
var article = new Article(req.body) | ||
article.user = req.user | ||
|
||
article.uploadAndSave(req.files.image, function (err) { | ||
if (err) { | ||
res.render('articles/new', { | ||
title: 'New Article', | ||
article: article, | ||
errors: err.errors | ||
}) | ||
} | ||
else { | ||
res.redirect('/articles/'+article._id) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Edit an article | ||
*/ | ||
|
||
exports.edit = function (req, res) { | ||
res.render('articles/edit', { | ||
title: 'Edit '+req.article.title, | ||
article: req.article | ||
}) | ||
} | ||
|
||
/** | ||
* Update article | ||
*/ | ||
|
||
exports.update = function(req, res){ | ||
var article = req.article | ||
article = _.extend(article, req.body) | ||
|
||
article.uploadAndSave(req.files.image, function(err) { | ||
if (err) { | ||
res.render('articles/edit', { | ||
title: 'Edit Article', | ||
article: article, | ||
errors: err.errors | ||
}) | ||
} | ||
else { | ||
res.redirect('/articles/' + article._id) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* View an article | ||
*/ | ||
|
||
exports.show = function(req, res){ | ||
res.render('articles/show', { | ||
title: req.article.title, | ||
article: req.article | ||
}) | ||
} | ||
|
||
/** | ||
* Delete an article | ||
*/ | ||
|
||
exports.destroy = function(req, res){ | ||
var article = req.article | ||
article.remove(function(err){ | ||
// req.flash('notice', 'Deleted successfully') | ||
res.redirect('/articles') | ||
}) | ||
} | ||
|
||
/** | ||
* List of Articles | ||
*/ | ||
|
||
exports.index = function(req, res){ | ||
var page = req.param('page') > 0 ? req.param('page') : 0 | ||
var perPage = 15 | ||
var options = { | ||
perPage: perPage, | ||
page: page | ||
} | ||
|
||
Article.list(options, function(err, articles) { | ||
if (err) return res.render('500') | ||
Article.count().exec(function (err, count) { | ||
res.render('articles/index', { | ||
title: 'List of Articles', | ||
articles: articles, | ||
page: page, | ||
pages: count / perPage | ||
}) | ||
}) | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var mongoose = require('mongoose') | ||
|
||
/** | ||
* Create comment | ||
*/ | ||
|
||
exports.create = function (req, res) { | ||
var article = req.article | ||
var user = req.user | ||
|
||
if (!req.body.body) return res.redirect('/articles/'+ article.id) | ||
|
||
article.addComment(user, req.body, function (err) { | ||
if (err) return res.render('500') | ||
res.redirect('/articles/'+ article.id) | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var mongoose = require('mongoose') | ||
, Article = mongoose.model('Article') | ||
|
||
/** | ||
* List items tagged with a tag | ||
*/ | ||
|
||
exports.index = function (req, res) { | ||
var criteria = { tags: req.param('tag') } | ||
var perPage = 5 | ||
var page = req.param('page') > 0 ? req.param('page') : 0 | ||
var options = { | ||
perPage: perPage, | ||
page: page, | ||
criteria: criteria | ||
} | ||
|
||
Article.list(options, function(err, articles) { | ||
if (err) return res.render('500') | ||
Article.count(criteria).exec(function (err, count) { | ||
res.render('articles/index', { | ||
title: 'List of Articles', | ||
articles: articles, | ||
page: page, | ||
pages: count / perPage | ||
}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.