Coder Social home page Coder Social logo

restful's Introduction

Experimental / Unreleased v0.0.0


# Restful

Build Status

Creates RESTful Director routers for resourceful models. Can be used as a stand-alone module or as a Flatiron plugin.

Installation

 npm install restful

Usage

var http        = require('http'),
    restful     = require('../lib/restful'),
    resourceful = require('resourceful');

//
// Create a new Creature resource using the Resourceful library
//
var Creature = resourceful.define('creature', function () {
  //
  // Specify a storage engine
  //
  this.use('memory');
  //
  // Specify some properties with validation
  //
  this.string('type');
  this.string('description');
});

//
// Create a new Director routing map based on "Creature" resource
//
var router = restful.createRouter(Creature);

//
// Setup a very simple HTTP server to serve our routing map!
//
var server = http.createServer(function (req, res) {
  req.chunks = [];
  req.on('data', function (chunk) {
    req.chunks.push(chunk.toString());
  });
  /*

   Router will now dispatch all RESTFul urls for the Creature resource

     POST    /Creature    => Creature.create()
     GET     /Creature    => Creature.all()
     GET     /Creature/1  => Creature.show()
     PUT     /Creature/1  => Creature.update()
     DELETE  /Creature/1  => Creature.destroy()

   Since not all HTTP clients support PUT and DELETE verbs ( such as forms in web browsers ),
   restful will also map the following browser friendly routes:

   If you prefer to not use this option, set { strict: true }

     POST  /Creature/1/update  => Creature.update()
     POST  /Creature/1/destroy => Creature.destroy()


  You might also want to consider using a rails-like approach which uses
  the convention of a reserved <form> input field called "_method" which contains either "PUT" or "DELETE"

    see: https://github.com/senchalabs/connect/blob/master/lib/middleware/methodOverride.js

  */
  router.dispatch(req, res, function (err) {
    if (err) {
      res.writeHead(404);
      res.end();
    }
    console.log('Served ' + req.url);
  });
});

server.listen(8000);

Tests

 npm test

TODO

  • Integrate as Flatiron plugin via app.use(flatiron.plugins.http.restful);
  • Full resourceful property type support ( numeric, boolean, array, object )
  • Full resourceful nested property schema support
  • Implement and document browser support
  • Improve Tests
  • Add better error support via errs library

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.