mog-api
v0.1.2
Published
Great googly Moogly, a small API
Downloads
3
Readme
MogAPI
Great googly Moogly, a small API.
This is a personal tool, it's probably no good for you. It was built for me to easily spin up and test API endpoints and prototype features on the fly.
Usage
const API = new MogAPI(host, port);
API.serve((Routes) => {
Routes.route("GET", /users/, getUsers);
Routes.route("POST", /users\/([0-9])\/update/, updateUsers);
});
MogAPI API
MogAPI(host, port)
class
Base class that lets you start the server for the environment.
Parameters
host
string (default:localhost
) - Set the host for the server to start on.port
number (default:8080
) - Set the port for the server to use.
MogAPI.serve(requestListener)
method
Creates the server, listens on the assigned port
and serves any content passed through the requestListener
callback.
Parameters
requestListener
RequestListener - The call back that is used for serving up content. An instance ofRoutes
is passed as the only accepted argument.
Routes(req, res)
class
Creates a routing instance to manage the API routes.
Parameters
req
any - A request object.res
any - A response object of the current request.
Routes.route(method, path, fn)
method
Long form assignment of a route against the current request.
Parameters
method
Method - The request method type.path
RegExp - Regular expression pattern to match against the request url.fs
CallbackFunction - The function to call when the method and path are valid.
Routes.get(path, fn)
method
Short for Routes.route('GET', /test/, () => {});
Parameters
path
RegExp - Regular expression pattern to match against the request url.fs
CallbackFunction - The function to call when the method and path are valid.
Routes.post(path, fn)
method
Short for Routes.route('POST', /test/, () => {});
Parameters
path
RegExp - Regular expression pattern to match against the request url.fs
CallbackFunction - The function to call when the method and path are valid.
Routes.handleResponse(response)
method
Handles the response content from a CallbackFunction
Parameters
response
Response - Regular expression pattern to match against the request url.
To Do
- Allow for any captured results and pattern matches to be passed back to the
CallbackFunction
.