npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

brainy-api

v0.0.3

Published

create a REST API from your Backbone models and collections

Downloads

4

Readme

brainy-api

brainy-api can create a RESTful api by simply analyzing Backbone models and collections. it can be included in your Express application, or you can run it's built-in server.

install

$ npm install brainy-api

use

using the library

brainy-api is a function which accepts two arguments: a reference to your Express application, and an array of Backbone models and collections. the models and collections are used to infer information about what APIs to build, and the app object is required to configure the actual routes.

require([
  'express',
  'backbone',
  'brainy-sync',
  'brainy-api'
], function(express, Backbone, Sync, Api) {

  var app = express();

  Backbone.sync = Sync({
    host: '127.0.0.1',
    port: 27017,
    name: 'brainy-api'
  });

  var User = Backbone.Model.extend({
    urlRoot: '/users'
  });

  Api(app, [User])

  app.listen(80);

});

important: the brainy-api library does not modify the behavior of Backbone.sync. this means you are responsible for overriding Backbone.sync with a server supported method before initializing brainy-api. for example, the built-in server (below) overrides sync for you, using brainy-sync.

running the built-in server

a built-in server is included which accepts a path to a directory of models and collections and creates a standalone REST server.

$ brainy-api --paths.resources=brainy-demo/src/js/resources

this will start a server exposing the http API methods (below). brainy-api accepts configuration options to describe the http server, the MongoDB connection, and resources directory. the default configuration looks like this:

{

  http: {
    port: 80
  },

  paths: {
    resources: null
  },

  db: {
    host: '127.0.0.1',
    port: 27017,
    name: 'brainy-api'
  }

}

brainy-api uses nconf (and by proxy optimist) to parse command line options. see optimist documentation for more information on overriding configuration. see the paths.resources example for reference.

methods

brainy-api uses a resource's url() to determine which endpoint each resource should be exposed by. this means setting your models urlRoot to /users will expose that model's methods at /users. the methods it exposes are inferred based on the resources type (model or collection).

models

when given a model, brainy-api will create methods for reading or creating models.

  • GET /:urlRoot/:id
  • POST /:urlRoot

collections

when given a collection, brainy-api will create a method for reading that collection.

  • GET /:url

todo

  • the api's external interface should act as middleware, while ideally removing the necessity to pass in the app object as an argument