New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

api-builder

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-builder

Build JSON API's in Node

  • 0.2.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 24 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc