API middleware for Node.js and Connect/Express.
Routing, and pre- and post-routing plugins for security, caching and logging.
Motivation for this module comes from attempts to find a more modular and flexible plugin to handle common API related funcationality.
npm install fillip
var fillip = require('fillip');
var express = require('express');
var redis = require('redis');
var api = require('routes/api');
var app = express();
fillip.initialize({
logging: true,
caching: {
type: 'redis',
db: redis
},
routes: {
hello: {
address: '/api/hello/:id',
controller: api.hello,
caching: true,
expiry: 300
}
}
});
app.get('/api/hello:id', function(req, res){
fillip.apicall(req, res);
});
Replace api.hello with your custom api function.
Your controller receives the request object and a callback function. The request object contains request parameters in the params associative array. Your controller should invoke the callback with the json object, as follows -
exports.hello = function(req, jsonCall){
var json = {
hello: 'world'
};
jsonCall(json);
};
Funcationality is provided through two primary methods. fillip.initialize() and fillip.apicall(). Currently only supports JSON responses.
Takes a javascript object with the following mandatory fields
- logging (boolean, either true or false)
- caching (javascript object)
- type (currently supports only 'redis')
- db (takes a redis object)
- routes (named objects with the following parameters, name must be unique)
- address (express.js route that will be handled through middleware)
- controller (your api function, must invoke a callback with the json object)
- caching (optional field, boolean, if true will store objects in db)
- expiry (if caching is set, this must be set as well, in seconds, accepts only numbers)
Invoke this method to let the middleware handle the request. Pass in express's request & response.
- Currently supports only one parameter URL (eg. /api/hello/:id )
- Logging functionality does not work yet
(The MIT License)
Copyright (c) 2013 Abdulla Faraz [email protected]
Refer to included LICENSE file.