Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/jsantell/swag-mvc
- Owner: jsantell
- License: mit
- Created: 2012-06-10T15:10:38.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2012-07-21T19:27:42.000Z (over 12 years ago)
- Last Synced: 2024-10-16T05:44:28.061Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 110 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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`