Define and build your project's api with mocked data.
It gives you enough control to:
You will need to create a json file with your API's definition. The file will need to have the following structure:
var express = ;var path = ;var http = ;var api = ;var app = ;app;// register your apiapi;http;
api : collection of api path definitions
api collection exppects a hash of objects, each
key is the actual path
that expressjs will match against each service created.
"/api/home" -> will be accesed at
This is optional, if defined, its a hash of header settings, the object will get
passed directly to express's
response.set() method, more info at:
The response object where the magic happens, it can be defined in multiple ways:
1 - most simple:
This will tell
api-template to respond with the content of the file being
mapped to. Currenty only supports text based files.
2 - rendered teamplte with json source:
Response will be the result of the content of
"data" json file passed into
3 - rendered teamplte with dynamic json source:
Same process as with option 2, the difference is that the json source will now be
passed through datafixture.js. This
is done by setting the flag
Because we are using ExpressJS we can generate dynamic routes.
ExpressJS passes a request object for each service, this one has all information passed into the url, including route variables and query variables. This object is passed into the JSON Object that is passed into a template, when template is defined. Giving you full access to it to be used in any way you want on the template.
Example of its implementation:
Making a call to
http://localhost:3000/api/isbn/SOME-ISBN-NUMBER its output
could look like: