Making a simple stubserver with Drakov
Today I'll show how you can create a simple stubserver with Drakov. If you do some front-end programming, you've probably already installed npm (Node Package Manager), otherwise here is how you install that.
<!-- more -->Then with npm you can install Drakov.
We can specify in a markdown file how the stubserver will behave. Using the rules of api-blueprint. This is normally used to specify what your API will look like and what inputs/outputs there are. Here we use it to actually create these in and outputs. First let start with some GETs.
After the ## we specify the paths. Underneath that we specify different methods (POST,GET). And how we will respond. So here we have 3 endpoints. On root (/) we return some documentation in text format. On /vegetables we return a list of vegetables. And specific vegetables under /vegetables/xxx We can start Drakov with this specification:
Now we can try some requests with curl:
This is an API about vegetables. With examples about how to get them.
Lets try some POSTs now.
A nice extra because it's a markdown file is that we can also let Markdown format it to make it even more readable. See 2-post.md Here we made it so that you can post any JSON object to /vegetables and return with http code 201 (created).
We say it needs to be JSON, so it will not work without the Content-Type header
It will also not work with incorrect JSON (no quotes around eggplant)
It will also not work with JSON that is not an object
We can also define some data structures to parse input.
Show a list (of one object) with the example object.
Shows the example object
The input will need to be a correct JSON object
Now it will fail on a JSON object that is not to our specifications
It will work with a correct vegetable
These examples are also available on https://github.com/tammosminia/blog-drakov