Mock Servers from API Specifications
Last week I mentioned an API specification called API Blueprint. Turns out not only does this allow us to document our API, but also use it.
That’s right, create a functioning mock server from that same API Blueprint used to document your API. I did mention this a bit in my previous post but I thought I’d put together a quick example.
sudo npm install -g drakov
I just grabbed the Polls API example from the blueprint examples.
I renamed the file to have a .apib extension. Run drakov like the following:
drakov -f PollsAPI.apib
Now you can access a mock server to test your front end code with! See the picture below for example.
If you’re running the mock server on a different host and from a browser, be sure to enable OPTIONS. To do this use the –autoOptions argument. This is because when the browser is accessing another host it will send a pre-flight OPTIONS request to see if the intended request is allowed. This bit me when creating my first NodeJS Express applications, I will always remember, vividly.
Use Notepad++ to find and replace tabs in your API Blueprint. Drakov can’t parse blueprints with tabs 🙁 .