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

api-builder

v0.2.4

Published

Build JSON API's in Node

Downloads

40

Readme

api-builder Build Status

Build API's in Node.

Load controllers, models and structures into optionally namespaced target(s).
Initialize routes from config/routes.js.
Easily add Redis backed sessions and gzipped Redis response caching.

Getting Started

Install the module with: npm install api-builder

Building your server.js

var apiBuilder = require('api-builder');
var express = require('express');
app = express();
app.use(express.cookieParser());

Use Redis for sessions
Requires a config/redis-session.yml, something like :

defaults: &defaults
  db: 2
  host: localhost
  port: 6379

development:
  <<: *defaults
  secret: qwertyuiop-dev:-)

production:
  <<: *defaults
  secret: qwertyuiop-prod:-)

then in your server.js

apiBuilder.redisSession(app, express);

Optionally cache responses in Redis, to be defined per controller function.
See sample controllers below for usage.
Requires a config/redis-cache.yml, something like :

defaults: &defaults
  host: localhost
  port: 6379

development:
  <<: *defaults
  password: redis

production:
  <<: *defaults
  password: hard-password

then in your server.js say

apiBuilder.cache.init(app, express);

Now some default Express stuff

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));

Make models available from the top level global namespace

apiBuilder.models.load('app/models', global);

Controllers namespaced by "controllers", eg. controllers.blah.blah

apiBuilder.controllers.load('app/controllers', global.controllers = {});

Now you can connect some defined routes to your controller functions

apiBuilder.routes.load(app, 'config/routes.yml', global.controllers);

Routes (config/routes.yml) might look like this :

- { path: '/',                            method: get,    action: Home.index }
- { path: '/:slug/:id/blogs',             method: get,    action: Blogs.show }
- { path: '/users/details',               method: get,    action: Users.details }
- { path: '/contact-us',                  method: post,   action: Messages.contactUs }

/app/controllers/home.js might look like :

module.exports = {
  index: function (req, res) {
    res.cache = 3600; // Cache this in Redis for one hour
    res.json({
      cheese: 'camembert'
    });
  },
  foo: function(req, res) {
    // This won't cache because we haven't defined a res.cache lifetime
    res.render('some/template', { cheese: 'edam' });
  }
};

Given :
/app/models/user.js
/app/models/user/roles.js
/app/models/user/profile.js

loaded as :

apiBuilder.models.load('app/models', global.models = {});

models are then accessed as :

models.User.someAttribute  
models.User.Roles.someAttribute  
models.User.Profile.someAttribute  

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2013 Mark Selby
Licensed under the MIT license.