Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    erapublic

    era

    REST adapter for express.

    • Current version: 0.0.3

    Important

    • Package under heavy modifications.
    • Authentication was disabled from version > 0.0.2.
    • Default behavior

    Installation

    $ npm install -g era
    

    About

    This package is intended for fast development of REST APIs.

    Quick start

    var era = require('era');
     
    var Api = era.Api;
    var Authentication = era.Authentication;
    var ModelResource = era.ModelResource;

    Declare a new resource

    function UserResource (model, authentication) {
      ModelResource.call(this, model, authentication);
    }
    util.inherits(UserResource, ModelResource);

    and a new api

    var api = new Api('api', 'v1');

    and register your new resource:

    api.register(new UserResource(models.User, Authentication));

    Now, for this resource you have working CRUD functionality and the urls /api/v1/user and /api/v1/user/:id are auto generated. The urls are base on the resource name, so the name has two parts, the model name, in our case is User and the word Resource, which is a mandatory word. If you want to change behavior of the resource you have to override get, post, put and delete methods.

    Routing with express

    The api object is the one we built before.

    var express = require('express');
    var app = express();
     
    app.use(express.logger());
    app.use(express.compress());
    app.use(express.bodyParser());
    app.use(app.router);
     
    api.adapter(app);

    Authentication

    Authentication have no rules so you'll have to extend it and implement isAuthenticated for setting your own rules.

    Here is an example:

    function ApiKeyAuth() {
      Authentication.call(this);
    };
    util.inherits(ApiKeyAuth, Authentication);
    ApiKeyAuth.prototype.isAuthenticated = function (req, res, next) {
      var apikey = req.query.apikey;
      if (apikey) {
        models.User.findOne({'apikey': apikey}).exec(function(err, obj) {
          if (obj) {
            next();
          }
          else {
            res.send(401);
          }
        });
      }
      else {
        res.send(401);
      }
    }

    Model

    The default CRUD behavior of the resource is built on top of mongoose. If you use something else you have to implement get, post, put and delete methods of your resource, so mongoose driver is not a requirement.

    Working with express

    Everything you'll extend with era is a middleware for express.

    License

    MIT

    Keywords

    none

    install

    npm i era

    Downloadslast 7 days

    5

    version

    0.0.3

    license

    none

    repository

    github.com

    last publish

    collaborators

    • avatar