api-builder
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 = ;var express = ;app = ;app;
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;
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
apiBuildercache;
Now some default Express stuff
app;app;app;app;
Make models available from the top level global namespace
apiBuildermodels;
Controllers namespaced by "controllers", eg. controllers.blah.blah
apiBuildercontrollers;
Now you can connect some defined routes to your controller functions
apiBuilderroutes;
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 :
moduleexports = { rescache = 3600; // Cache this in Redis for one hour res; } { // This won't cache because we haven't defined a res.cache lifetime res; };
Given :
/app/models/user.js
/app/models/user/roles.js
/app/models/user/profile.js
loaded as :
apiBuildermodels;
models are then accessed as :
modelsUsersomeAttribute modelsUserRolessomeAttribute modelsUserProfilesomeAttribute
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.