Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Replaced the default mongodb driver with mongoose. Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
petecoop committed Jul 23, 2013
1 parent 32501d6 commit ecb784e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 57 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ An Expressjs generator for Yeoman, based on the express command line tool.
- Run: `grunt` to run the local server at `localhost:3000`, the grunt tasks include live reloading for .jade views, css in public/stylesheets and restarting the server for changes to app.js or js in routes/

## MVC apps
I've created a new generator for creating MVC style apps in express, it's based around [nodejs-express-mongoose-demo](https://github.com/madhums/nodejs-express-mongoose-demo) however I've included the mongodb-native driver rather than mongoose, this can easily be changed and may be added as an option further down the line.
I've created a new generator for creating MVC style apps in express, it's based around [nodejs-express-mongoose-demo](https://github.com/madhums/nodejs-express-mongoose-demo). It uses [MongoDB](http://www.mongodb.org/) as it's default database, you will need to have it installed and running to get the default app running.

To get going:
- Make sure you have [yo](https://github.com/yeoman/yo) installed:
`npm install -g yo`
- Install the generator **locally**: `npm install generator-express`
- Run: `yo express:mvc`
- Ensure that MongoDB is running on your machine, if running elsewhere the connection string can be changed in `config/config.js`
- Run: `grunt` to run the local server - defaults to `localhost:3000` - port can be changed in `config/config.js`. The grunt tasks include live reloading as before.

##Options
Expand All @@ -26,6 +27,13 @@ To get going:
Skips the automatic execution of `bower` and `npm` after
scaffolding has finished.

##Testing
Tests are written with mocha.
- Install: `npm install -g mocha`
- Run: `mocha`

##Contributing
Contributors are welcome, please fork and send pull requests! If you have any ideas on how to make this project better then please submit an issue.

## License
[MIT License](http://en.wikipedia.org/wiki/MIT_License)
4 changes: 4 additions & 0 deletions mvc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ ExpressGenerator.prototype.setupEnv = function setupEnv() {
ExpressGenerator.prototype.packageJSON = function packageJSON() {
this.template('package.json', 'package.json');
};

ExpressGenerator.prototype.config = function config() {
this.template('config/config.js', 'config/config.js');
};
2 changes: 1 addition & 1 deletion templates/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"start": "node app"
},
"dependencies": {
"express": "~3.0.0",
"express": "~3.3.4",
"jade": "*"
},
"devDependencies": {
Expand Down
17 changes: 16 additions & 1 deletion templates/mvc/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
var express = require('express'),
mongoose = require('mongoose'),
fs = require('fs'),
config = require('./config/config');

mongoose.connect(config.db);
var db = mongoose.connection;
db.on('error', function(){
throw new Error('unable to connect to database at ' + config.db);
});

var models_path = __dirname + '/app/models';
fs.readdirSync(models_path).forEach(function(file){
if(file.indexOf('.js') >= 0){
require(models_path + '/' + file);
}
});

var app = express();

require('./config/express')(app, config);
require('./config/routes')(app);

app.listen(config.port);
app.listen(config.port);
13 changes: 10 additions & 3 deletions templates/mvc/app/controllers/home.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
var mongoose = require('mongoose'),
Article = mongoose.model('Article');

exports.index = function(req, res){

res.render('home/index', {
title: 'Generator-Express MVC',
});
Article.find(function(err, articles){
if(err) throw new Error(err);
res.render('home/index', {
title: 'Generator-Express MVC',
articles: articles
});
});
};
28 changes: 12 additions & 16 deletions templates/mvc/app/models/article.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
// Example model

var MongoClient = require('mongodb').MongoClient,
Db = require('../../config/database');
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

module.exports = new Article();
var ArticleSchema = new Schema({
title: String,
url: String,
text: String
});

function Article(){
this.collection_name = "articles";
}

Article.prototype.findAll = function(callback){
Db.getCollection(this.collection_name, function(collection){
collection.find().toArray(function(err, items){
items.forEach(function(item){
item.date = getDate(item._id);
});
callback(items);
});
ArticleSchema.virtual('date')
.get(function(){
return this._id.getTimestamp();
});
};

mongoose.model('Article', ArticleSchema);
4 changes: 2 additions & 2 deletions templates/mvc/config/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
root: require('path').normalize(__dirname + '/..'),
app: {
name: 'Application-name'
name: '<%= _.slugify(appname) %>'
},
port: 3000,
db: 'mongodb://localhost/database-name'
db: 'mongodb://localhost/<%= _.slugify(appname) %>'
};
30 changes: 0 additions & 30 deletions templates/mvc/config/database.js

This file was deleted.

4 changes: 2 additions & 2 deletions templates/mvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"start": "app.js"
},
"dependencies": {
"express": "3.0.3",
"express": "~3.3.4",
"jade": "*",
"mongodb": "~1.3.0"
"mongoose": "~3.6.14"
},
"devDependencies": {
"grunt": "~0.4.1",
Expand Down
1 change: 0 additions & 1 deletion test/test-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ describe('express MVC generator', function () {
'public/css',
'package.json',
'config/config.js',
'config/database.js',
'config/express.js',
'config/routes.js',
'app/controllers',
Expand Down

0 comments on commit ecb784e

Please sign in to comment.