How it works
Quick guide to learn the basics about Mockly
Mockly is all about REST resources (or not, as you will see later) and their HTTP methods to create, retrieve, update and delete its information. Therefore, there should be an easy way to define the resources you need available to fill the requirements of your application.
Defining resources
To discover how to change the pattern used to find files with resources definition see the configuration page.
To define a new resource you only need to create a JSON file and name it using this pattern: {name}.resource.json
. We recommend as convention to use a folder named api
in the project root folder. Also, if you have multiple resources definitions you probably want to store them in a folder named resources
.
Inside the file you have to create a JSON object with an unique key representing the resource name and its default values. In the follow example, we are creating the artists resource with a collection of artists:
And that's it! Now you can move to the root folder and type mockly
to start the server:
Now the server is up and the resources available in /artists
path. You can open http://localhost:3000/artists to see the created resources!
So, what is going on under the hood? Mockly exposes all the routes and HTTP methods to accomplish basic CRUD operations. By default, you will have the following requests available:
Path
Method
Description
/artists
GET
Retrieves all artists
/artists
POST
Creates an artist
/artists/:id
GET
Gets artist info
/artists/:id
PUT
/ PATCH
Updates artist info
/artists/:id
DELETE
Deletes artist
Easy peasy! Now you can start making requests to the artists resource and it only takes a couple of minutes!
But, what happen if your application services are not following the RESTful paradigm? What if you only want to define a few endpoints with a mocked response? Let's see it.
Defining data endpoints
Use simple key names and use rewrites to fit your needs
Defining data endpoints it's something trivial as we do with resources. At this time, this kind of data follows the same pattern as resources files so you only need to create a file and put in it a key with a value. For example:
Here we are defining me
key data with a JSON response, keep in mind that this key is used to generate the endpoint so when you start the server this data will be available in /me
path. With this kind of data Mockly exposes only two HTTP methods: GET
and POST
.
Last updated