Skip to content

larous25/rode

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RodeJS

Smart Packet-Oriented Framework for Express and Mongoose.

##Table of Contents:

Quick Start

Installing Rode and setting up your application:

# npm install -g rode bower grunt-cli
$ rode generate --sessions --css stylus --ejs myapp

Install dependencies:

$ npm install

Start the server:

$ node app

The usage of rode generate is nearly the same that Express

Usage: rode generate [options] [dir]

Options:

  -h, --help          output usage information
  -V, --version       output the version number
  -s, --sessions      add session support
  -e, --ejs           add ejs engine support (defaults to jade)
  -H, --hogan         add hogan.js engine support
  -S, --soy           add Google Closure Templates engine support
  -c, --css           add stylesheet support (less|stylus) (defaults to plain css)
  -f, --force         force on non-empty directory

Packages

A package (or bundle) is a component of your application with its own MVC.

You can simply create a new package with the command:

$ rode new:package PackageName

Usage: rode new:package <package>

Options:

  -h, --help   output usage information
  -r, --rest   config rest api for this package
  -f, --force  force on existing package

Models

Models on Rode are modules really extensibles.

var rode = require('rode');

var Model = rode.getBaseModel();

var Person = Model.extend({
  name: 'Person',
  sayHello: function () {
    console.log('Hello Models!');
  }
});

var Employee = Person.extend({
  name: 'Employee'
});

Employee.sayHello(); // Hello Models!

Models with Mongoose

In this example we first define Person Model and their Schema:

var rode = require('rode');

var Model = rode.getBaseModel();

var Person = Model.extend({
  name: 'Person',
});
Person.setSchema({
  name: {
    type: 'String',
    unique: true
  },
  age: {
    type: 'Number',
    default: 0
  }
});

Now define Employee Model and their Schema extending from Person:

var Employee = Person.extend({
  name: 'Employee'
});
Employee.setSchema({
  company: 'String'
});

You can create some persons and employees:

var john = new Person.model({
  name: 'John',
  age: 40
});
var mariano = new Employee.model({
  name: 'Mariano Pardo',
  age: '22',
  company: 'Codexar'
});

And you can still using any mongoose method:

// Save John on Persons
john.save(function (err) {
  // Save Mariano on Employees
  mariano.save(function (err) {
    // Get all persons (Persons + Employees)
    Person.getAll(function (err, persons) {
      console.log(persons);
      // You can access to Mongoose model
      Person.model.remove({});
    });
  });
});

We recommend having a single file for each model.

Controllers

Controllers and Routes work together to facilitate the routing of the application.

A controller looks like:

var rode = require('rode');

var HelloController = {
  /**
   * sayHello Action
   */
  sayHello: function (req, res) {
    res.render('index', {
      title: 'Hello World!'
    });
  }
};

module.exports = HelloController;

And Route for this Controller looks like:

var rode = require('rode');

var mainRouter = rode.getRouter('Main');

/**
 * Base Route for this Package (Optional)
 */
mainRouter.setBase('/');

/**
 * Responds to /hello with MainController.sayHello
 */
mainRouter.add({
  controller: 'Hello', // Defaults 'Main'
  pattern: 'hello',
  action: 'sayHello',
  method: 'get' // Defaults 'all'
});

Middleware on Routes

Here's an example of how to define a middleware on routes:

var UserController = {
  showPrivateData: [
    function (req, res, next) {
      // Check permissions
      next();
    },
    function (req, res) {
      // Show Private Data
    }
  ],
};

Restful APIs

Make a Restful API can not be more easy. Create your package with the command:

$ rode new:package PackageName --rest

Or add productRouter.setRestApi('/api/products/'); on routes.js

Now you should create methods on your RestController.js following simple naming conventions.

Here are some examples:

// [GET] /api/products
get: function (req, res) { ... }

// [POST] /api/products
post: function (req, res) { ... }

// [GET] /api/products/sponsored
getSponsored: function (req, res) { ... }

// [PUT] /api/products/sponsored
putSponsored: function (req, res) { ... }

// [POST] /api/products/sponsored/today
postSponsoredToday: function (req, res) { ... }

// [GET] /api/products/:id
getById: function (req, res) { ... }

// [POST] /api/products/:id
postById: function (req, res) { ... }

// [DELETE] /api/products/:id
deleteById: function (req, res) { ... }

// [GET] /api/products/:id/comments
getByIdComments: function (req, res) { ... }

// [PUT] /api/products/:id/comments/:id2
putByIdCommentsById: function (req, res) { ... }

You can create as many combinations as you like. Remember that each package can have its own RestController.js

Tests

You can run all the test with the command:

$ grunt test

Templates engines

Rode supports all this templates engines:

About

Smart Packet-Oriented Framework for Express and Mongoose.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published