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.
const API = new MogAPI(host, port);
API.serve((Routes) => {
Routes.route("GET", /users/, getUsers);
Routes.route("POST", /users\/([0-9])\/update/, updateUsers);
});
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.
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.
Creates a routing instance to manage the API routes.
Parameters
req
any - A request object.res
any - A response object of the current request.
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.
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.
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.
Handles the response content from a CallbackFunction
Parameters
response
Response - Regular expression pattern to match against the request url.
- Allow for any captured results and pattern matches to be passed back to the
CallbackFunction
.