quick and simple web api generator
var express = require'express'apitizer = require'apitizer';var app = express;appuseexpressbodyParser;var api = apitizernamespace: 'my.test.api'path: '/myapi.js';appuseapiprovider;appget'/'ressendfile'test.html';;callok'add': a + b + c'sub': a - b - c;apiextendmyApiFunctionextend 'SOMEVALUE': 3832.1234 ;applisten8080;
via npm (recommended)
npm install apitizer --production
git clone https://github.com/farwyler/node-apitizer.git apitizer cd apitizer npm install --production
The apitizer module is the constructor function for interface objects. You can have multiple of these but it is your responsibility to manage their namespace and path to avoid collisions.
namespace- The browser namespace your functions will be placed in. Nesting is possible via '.', e.g. 'my.cool.api' will be recursively created in the global namespace of your browser.
path- The path at which your api will be served.
methd- The http request method to use,
'post'. Defaults to
'post'. Note: Using
'post'requires you to add the
bodyParsermiddleware to your express app.
var apitizer = require'apitizer';var myapi = apitizernamespace: 'my.test.api'path: '/myapi.js';
Adds elements to the interface. Returns the interface object to allow chain calls, e.g. myapi.extend(func1).extend(func2).
object- Accepts a single function or an object. Objects will be iterated through and all contained functions added to the interface.
//...// make myApiFunction available to the browsermyapiextendmyApiFunction;// add some constantsmyapiextenda_string: 'hello world!'a_number: 39383.98123a_bool: false;// add a whole modulemyapiextendrequire'./myapi.js';
Registers your interface as a middleware to express.
Important: The default http request method is POST, which requires you to add the
bodyParser middleware to your express app.
expressappuseexpressbodyParser; // you dont need this if you are using GET requestsexpressappusemyapiprovider;
Functions exposed to the browser, have no limitation in argument count but always receive as last argument a CallObject. A CallObject contains detailed contextual information about the call to be made and can be inspected/extended by registering a hook function. It also contains methods (
ok(...)) to tell the interface that your function has finished and the response to send to the browser.
if Mathrandom < 0.5callfail'so unlucky!';elsecallok'add': a + b + c'sub': a - b - c;
Calling the function from the browser
mytestapimyApiFunction23 42 966if errconsole.log':(' err;elseconsole.logresultadd resultsub;
A CallObject is guaranteed to contain the following:
The original connect/express request object.
The original connect/express response object.
Name of the function to be called.
Function arguments received by the browser.
Function to end the call unsuccessfully with a given message.
Function to end the call successfully and return the result to the browser. Note: The result will be send as JSON.
Performs the actual call to the interface function.
Important: This method has to be called when using a
call hook and nowhere else.
'call'- Called with the CallObject before each call to an interface function. Can be used to extend the CallObject, e.g. with additional authentication information, if not already present in the express request object. Can also prematurely end the call by calling
ok(...), in which case the actual function will not be executed. Important: If you register this hook, it will be your responsibility to trigger further processing of the call, by executing the
CallObject.execute()after you are done.
myapihook'call'// do not allow calls to specific functionif coname === 'badFunction'cofail'not allowed';return;// add user authentication information to the CallObjectcouserinfo =name: 'John Doe'secret: 'Cats';coexecute;;if calluserinfosecret === 'Cats'callok;elsecallfail'wrong!';