resourcejs

A RESTful Resource base class for Express.js.

npm install resourcejs
2 downloads in the last week
5 downloads in the last month

Resource.js - A RESTful base class for Express.js.

Resource.js is designed to be a base class for RESTful web services using Express.js and MongoJS, all within the Node.js platform. It is an easy way to easily create your restful object SCHEMA's where it then builds out the CRUD operations for you. After you derive your object from this base class, and then mount that resource to a provided path, all of the following endpoints will be established for you. This example assumes that you mount your derived resource at the resource path.

  • /resource - (GET) - The index of all resources (JSON).
  • /resource - (POST) - Add a new resource.
  • /resource/index - (GET) - HTML version of the index.
  • /resource/add - (GET) - HTML add form
  • /resource/:id - (GET) - Returns the data of specific resource (JSON).
  • /resource/:id - (POST) - Updates an existing resource object.
  • /resource/:id - (DELETE) - Deletes an existing resource.
  • /resource/:id/view - (GET) - View a resource HTML.
  • /resource/:id/edit - (GET) - Edit resource form HTML.
  • /resource/:id/delete - (GET) - Delete resource form HTML.

Below is an example of how to use this class.

Example 1

Let's suppose you wish to define a User object which is accessed from the 'user' endpoint. You would then just create a User.js file within the root of your Node.js project and it would look like the following.

// Define our Resource class.
var Resource = require('./node_modules/resource/Resource.js');

/**
 * Constructor
 *
 * @extends Ressource
 *
 * @param {object} object The object representation of this resource.
 * @param {object} options The options to configure how this resource behaves.
 * @param {function} callback Called when the resource is finished loading.
 */
exports = module.exports = function(object, options, callback) {
  Resource.call(this, object, options, callback);
};

// Boilerplate code to extend the Resource class.
exports.prototype = new Resource();
exports.prototype.constructor = exports;

/**
 * Define the schema for our User object.
 */
exports.prototype.schema = function() {

  // Return the Resource class's schema merged in with ours.
  return Resource.merge(Resource.prototype.schema.call(this), {
    first: 'string',
    last: 'string',
    email: 'string'
  });
};

/**
 * The form for the fields of this resource.
 */
exports.prototype.fieldForm = function(path) {

  // Return the output for the form to create a new resource.
  var output = Resource.prototype.fieldForm.call(this);
  output += 'First name: <input type="text" name="first" value="' + this.get('first') + '" /><br />';
  output += 'Last name: <input type="text" name="last" value="' + this.get('last') + '" /><br />';
  output += 'Email: <input type="text" name="email" value="' + this.get('email') + '" /><br />';
  output += '<input type="submit" value="Save" />';
  return output;
};

You would then just need to create the following within your app.js file in order to mount this Object to a specific path, like so..

var express =   require('express');
var mongojs =   require('mongojs');

// Create the database.
var db = mongojs.connect("localhost/test", ["users"]);

// Create the app.
var app = express();
app.use(express.bodyParser());

// Require the Resource.
var Resource = require('./node_modules/resource/Resource.js');

// Establish the user object.
var User = require('./User.js');

// Mount the user resource.
Resource.mount(app, db.users, 'user', User);

// Listen to port 3000.
app.listen(3000);
console.log('Express app started on port 3000');

Once you run this app using node app.js, this will then mount the User object (which derives from Resource) on the path of localhost:3000/user.

You will then have all of the following RESTful endpoints exposed to this object.

  • /user - (GET) - The index of all users (JSON).
  • /user - (POST) - Add a new user.
  • /user/index - (GET) - HTML version of the index.
  • /user/add - (GET) - HTML add form
  • /user/:id - (GET) - Returns the data of specific user (JSON).
  • /user/:id - (POST) - Updates an existing user object.
  • /user/:id - (DELETE) - Deletes an existing user.
  • /user/:id/view - (GET) - View a user HTML.
  • /user/:id/edit - (GET) - Edit user form HTML.
  • /user/:id/delete - (GET) - Delete user form HTML.

More documentation comming soon...

npm loves you