0.1.2 • Published 3 years ago
mog-api v0.1.2
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
- hoststring (default:- localhost) - Set the host for the server to start on.
- portnumber (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
- requestListenerRequestListener - The call back that is used for serving up content. An instance of- Routesis passed as the only accepted argument.
Routes(req, res) class
Creates a routing instance to manage the API routes.
Parameters
- reqany - A request object.
- resany - A response object of the current request.
Routes.route(method, path, fn) method
Long form assignment of a route against the current request.
Parameters
- methodMethod - The request method type.
- pathRegExp - Regular expression pattern to match against the request url.
- fsCallbackFunction - The function to call when the method and path are valid.
Routes.get(path, fn) method
Short for Routes.route('GET', /test/, () => {});
Parameters
- pathRegExp - Regular expression pattern to match against the request url.
- fsCallbackFunction - The function to call when the method and path are valid.
Routes.post(path, fn) method
Short for Routes.route('POST', /test/, () => {});
Parameters
- pathRegExp - Regular expression pattern to match against the request url.
- fsCallbackFunction - The function to call when the method and path are valid.
Routes.handleResponse(response) method
Handles the response content from a CallbackFunction
Parameters
- responseResponse - 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.