Skip to content

Instantly share code, notes, and snippets.

@robert52
Created September 20, 2012 21:51
Show Gist options
  • Save robert52/3758573 to your computer and use it in GitHub Desktop.
Save robert52/3758573 to your computer and use it in GitHub Desktop.
var flatiron = require('flatiron'),
resourceful = require('resourceful'),
plates = require('plates'),
path = require('path'),
ecstatic = require('ecstatic'),
app = flatiron.app,
config,
m = {};
/**
* Config file
*/
app.config.file({ file: path.join(__dirname, './config/config.json')});
/**
* Flatiron plugins
*/
app.use(flatiron.plugins.http, {
before: [
ecstatic(path.join(__dirname, './public'), {autoIndex: false})
]
});
app.use(flatiron.plugins.resourceful, {
//root: __dirname,
engine: 'memory'
});
/**
* Registering Models
*/
['user'].forEach(function(model) {
require('./app/models/' + model + '_model')(resourceful);
});
/**
* Registering routers
*/
['main', 'user'].forEach(function(router) {
require('./app/routers/' + router + '_router')(app, resourceful, plates, config);
});
app.start(app.config.get('port') || 3000, function(err) {
if (err) {
throw err;
}
var addr = app.server.address();
app.log.info('Listening on http://' + addr.address + ':' + addr.port);
});
//user_model.js
module.exports = function(resourceful) {
var User = resourceful.define('user', function() {
this.string('username').required(true);
this.string('password').required(true);
this.string('email', { format: 'email', requierd: true});
});
return User;
};
//user_router.js
var UserRouter = function(app, resourceful, plates, config) {
var resources = resourceful.resources;
//this one works but can .save() the resource
// var user = new(resources.User)({
// username: 'Bob',
// password: 'bob!234',
// email: '[email protected]'
// });
var user = resources.User.create({
id : 'Test'
}, function(err, user) {
user = {
username: 'Bob',
password: 'bob!234',
email: '[email protected]'
};
console.log(err, user);
user.save(function(err, result) {
console.log(err, result);
});
});
/**
* Get all users
*/
app.router.get('/users', function() {
var users = resources.User.all();
this.res.json(200, users);
});
};
module.exports = UserRouter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment