Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jsantell/swag-mvc

connecting your models and routes in node.js
https://github.com/jsantell/swag-mvc

Last synced: about 2 months ago
JSON representation

connecting your models and routes in node.js

Awesome Lists containing this project

README

        

swag-mvc
======

connecting your models and routes in node.js

### Installing

* `npm install swag-mvc`

### Creating

* `var mvc = require('mvc')( app )` Pass in `express.createServer()`

### Models

* `mvc.initModels( models, dir, setupFunction, args )` sets up models in `dir` defined in the array of strings `models`. `setupFunction` has two arguments, one is the capitalized name of the model and the other is the return value of the model file in `dir/ModelName` to hook it into your database of choice. `args` are arguments passed into the model file.

### Routes

* `mvc.initRoutes( routes, dir, args )` sets up the routes in `dir`, defined with an array of strings `routes`, passing in `app`, corresponding model if applicable, followed by additional `args`.

Example
---
`app.js`
```javascript
mvc = require('mvc')( app );
db = mongoose.connect( url );

mvc.initModels( [ 'User' ], __dirname + '/models/', function ( name, schema ) {
db.model( name, schema );
return db.model( name );
}, {
mongoose: mongoose
});

mvc.initRoutes( [ 'users', 'pages', 'sesions' ], __dirname + '/controllers/', {
auth: function ( req, res, next ) {
req.isAuthenticated() ? next() : res.redirect( '/login' );
});
});
```

`ROOT/controllers/users.js`
```javascript
// Passing in arguments app, the model, and arguments object passed in
// during initRoutes
module.exports = function ( app, User, args ) {
app.get( '/users', function ( req, res, next ) {
User.find( {}, ( err, users ) {
if ( !err ) { res.render( 'users/index', { users: users } }
});
});

app.get( '/register', function ( req, res, next ) {
res.render( '/users/new' );
});
};
```

`ROOT/models/User.js`
```javascript
module.exports = function ( args ) {
mongoose = args.mongoose;
ObjectId = mongoose.SchemaTypes.ObjectId;

UserSchema = new mongooose.Schema({});

// this is the schema passed into the
// setupFunction in initModels
return UserSchema;
};
```

Testing
---

Run `node tests/runTests.js` from project root -- testing uses `nodeunit`