Skip to content

Commit

Permalink
Initial Mean.io commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermcginnis committed Oct 2, 2013
1 parent 2aa0aa0 commit 2f54ead
Show file tree
Hide file tree
Showing 69 changed files with 1,784 additions and 2 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: ./node_modules/.bin/forever -m 5 server.js
123 changes: 121 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,121 @@
cardsforhumanity
================
# MEAN Stack

MEAN is a boilerplate that provides a nice starting point for [MongoDB](http://www.mongodb.org/), [Node.js](http://www.nodejs.org/), [Express](http://expressjs.com/), and [AngularJS](http://angularjs.org/) based applications. It is designed to give you quick and organized way to start developing of MEAN based web apps with useful modules like mongoose and passport pre-bundled and configured. We mainly try to take care of the connection points between existing popular frameworks and solve common integration problems.

## Prerequisites
* Node.js - Download and Install [Node.js](http://www.nodejs.org/download/). You can also follow [this gist](https://gist.github.com/isaacs/579814) for a quick and easy way to install Node.js and npm
* MongoDB - Download and Install [MongoDB](http://www.mongodb.org/downloads) - Make sure it's running on the default port (27017).

### Optional
* Grunt - Download and Install [Grunt](http://gruntjs.com).

## Additional Packages
* Express - Defined as npm module in the [package.json](package.json) file.
* Mongoose - Defined as npm module in the [package.json](package.json) file.
* Passport - Defined as npm module in the [package.json](package.json) file.
* AngularJS - Defined as bower module in the [bower.json](bower.json) file.
* Twitter Bootstrap - Defined as bower module in the [bower.json](bower.json) file.
* UI Bootstrap - Defined as bower module in the [bower.json](bower.json) file.

## Quick Install

The quickest way to get started with MEAN is to clone the project and utilize it like this:

Install dependencies:

$ npm install

Export the node Path to load your lib into project (default in HEROKU)
$ export NODE_PATH=lib

We recommend using [Grunt](https://github.com/gruntjs/grunt-cli) to start the server:
$ grunt

When not using grunt you can use:

$ node server

Then open a browser and go to:

http://localhost:3000

## Quick Deployment
4 commands to deploy your mean app to heroku,
Before you start make sure you have <a href="https://toolbelt.heroku.com/">heroku toolbelt</a> installed and an accessible mongo db instance - you can try <a href="http://www.mongohq.com/">mongohq</a> which have an easy setup )

```bash
git init
git add .
git commit -m "initial version"
heroku apps:create
git push heroku master
```

## Configuration
All configuration is specified in the [config](config/) folder, particularly the [config.js](config/config.js) file. Here you will need to specify your application name, database name, as well as hook up any social app keys if you want integration with Twitter, Facebook, GitHub or Google.

### Environmental Settings

There are three environments provided by default, __development__, __test__, and __production__. Each of these environments has the following configuration options:
* db - This is the name of the MongoDB database to use, and is set by default to __mean-dev__ for the development environment.
* root - This is determined automatically at the start of this file, but can be overridden here.
* app.name - This is the name of your app or website, and can be different for each environment. You can tell which environment you are running by looking at the TITLE attribute that your app generates.
* Social Registration - Facebook, GitHub, Google, Twitter. You can specify your own social accounts here for each social platform, with the following for each provider:
* clientID
* clientSecret
* callbackURL

To run with a different environment, just specify NODE_ENV as you call grunt:

$ NODE_ENV=test grunt

If you are using node instead of grunt, it is very similar:

$ NODE_ENV=test node server

> NOTE: Running Node.js applications in the __production__ environment enables caching, which is disabled by default in all other environments.
## Getting Started
We pre-included an article example, check it out:
* [The Model](https://github.com/linnovate/mean/blob/master/app/models/article.js) - Where we define our object schema.
* [The Controller](https://github.com/linnovate/mean/blob/master/app/controllers/articles.js) - Where we take care of our backend logic.
* [NodeJS Routes](https://github.com/linnovate/mean/blob/master/config/routes.js) - Where we define our REST service routes.
* [AngularJs Routes](https://github.com/linnovate/mean/blob/master/public/js/config.js) - Where we define our CRUD routes.
* [The AngularJs Service](https://github.com/linnovate/mean/blob/master/public/js/services/articles.js) - Where we connect to our REST service.
* [The AngularJs Controller](https://github.com/linnovate/mean/blob/master/public/js/controllers/articles.js) - Where we take care of our frontend logic.
* [The AngularJs Views Folder](https://github.com/linnovate/mean/blob/master/public/views/articles) - Where we keep our CRUD views.

## MEAN Modules
Mean presents a growing eco-system of MEAN based modules in the npm repository, To write (and contribute) your own MEAN based module checkout [mean-logger](https://npmjs.org/package/mean-logger) for examples.

## More Information

* Contact Amos Haviv on any issue via [E-Mail](mailto:mail@amoshaviv.com), [Facebook](http://www.facebook.com/amoshaviv), or [Twitter](http://www.twitter.com/amoshaviv).
* Visit us at [Linnovate.net](http://www.linnovate.net/).
* Visit our [Ninja's Zone](http://www.meanleanstartupmachine.com/) for extended support.

## Credits
Inspired by the great work of [Madhusudhan Srinivasa](https://github.com/madhums/)

## License

(The MIT License)

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.
91 changes: 91 additions & 0 deletions app/controllers/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
async = require('async'),
Article = mongoose.model('Article'),
_ = require('underscore');


/**
* Find article by id
*/
exports.article = function(req, res, next, id) {
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();
});
};

/**
* Create a article
*/
exports.create = function(req, res) {
var article = new Article(req.body);
article.user = req.user;

article.save(function(err) {
if (err) {
return res.send('users/signup', {
errors: err.errors,
article: article
});
} else {
res.jsonp(article);
}
});
};

/**
* Update a article
*/
exports.update = function(req, res) {
var article = req.article;

article = _.extend(article, req.body);

article.save(function(err) {
res.jsonp(article);
});
};

/**
* Delete an article
*/
exports.destroy = function(req, res) {
var article = req.article;

article.remove(function(err) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(article);
}
});
};

/**
* Show an article
*/
exports.show = function(req, res) {
res.jsonp(req.article);
};

/**
* List of Articles
*/
exports.all = function(req, res) {
Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(articles);
}
});
};
13 changes: 13 additions & 0 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
async = require('async'),
_ = require('underscore');


exports.render = function(req, res) {
res.render('index', {
user: req.user ? JSON.stringify(req.user) : "null"
});
};
103 changes: 103 additions & 0 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
User = mongoose.model('User');

/**
* Auth callback
*/
exports.authCallback = function(req, res, next) {
res.redirect('/');
};

/**
* Show login form
*/
exports.signin = function(req, res) {
res.render('users/signin', {
title: 'Signin',
message: req.flash('error')
});
};

/**
* Show sign up form
*/
exports.signup = function(req, res) {
res.render('users/signup', {
title: 'Sign up',
user: new User()
});
};

/**
* Logout
*/
exports.signout = function(req, res) {
req.logout();
res.redirect('/');
};

/**
* Session
*/
exports.session = function(req, res) {
res.redirect('/');
};

/**
* Create user
*/
exports.create = function(req, res) {
var user = new User(req.body);

user.provider = 'local';
user.save(function(err) {
if (err) {
return res.render('users/signup', {
errors: err.errors,
user: user
});
}
req.logIn(user, function(err) {
if (err) return next(err);
return res.redirect('/');
});
});
};

/**
* Show profile
*/
exports.show = function(req, res) {
var user = req.profile;

res.render('users/show', {
title: user.name,
user: user
});
};

/**
* Send User
*/
exports.me = function(req, res) {
res.jsonp(req.user || null);
};

/**
* Find user by id
*/
exports.user = function(req, res, next, id) {
User
.findOne({
_id: id
})
.exec(function(err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
next();
});
};
51 changes: 51 additions & 0 deletions app/models/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
config = require('../../config/config'),
Schema = mongoose.Schema;


/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true
},
content: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});

/**
* Validations
*/
ArticleSchema.path('title').validate(function(title) {
return title.length;
}, 'Title cannot be blank');

/**
* Statics
*/
ArticleSchema.statics = {
load: function(id, cb) {
this.findOne({
_id: id
}).populate('user', 'name username').exec(cb);
}
};

mongoose.model('Article', ArticleSchema);
Loading

0 comments on commit 2f54ead

Please sign in to comment.