Mock-Server whith nodejs
In my last works, I have been worked integrations with partners and a common scenario is:
The partner provides documentation and a homologation endpoint. At this point, it is clear how to use the API and it is clear the desired response, but….
The homolog server doesn’t work properly :(
In this case, I have been used the json-server to make my fake homolog server.
A simple example of how it works:
Suppose that the end-point are /users
and /dogs
.
You just need to create a file, for example, db.json
and put something like that:
{
"users": [
{
"id": 1,
"name": "Ana"
},
{
"id": 2,
"name": "Helio"
}
],
"dogs": [
{
"id": 1,
"name": "Dorothy"
},
{
"id": 2,
"name": "Bebel"
}
]
}
Now, install the json-server
npm install -g json-server
and run the follow command:
json-server -p 3002 --watch db.json
curl --request GET \
--url http://localhost:3002/users
result:
[
{
"id": 1,
"name": "Ana"
},
{
"id": 2,
"name": "Helio"
}
]
curl --request GET \
--url http://localhost:3002/users/1
result:
{
"id": 1,
"name": "Ana"
}
curl --request GET \
--url http://localhost:3002/dogs/1
result:
{
"id": 1,
"name": "Dorothy"
}
But in real-world endpoints are not so simple.Suppose that instead /users
the endpoint is /api/v1/customers
.
You can make a rote file to address this problem. The file is something like that:
routes.js
{
"/api/v1/customers/:id": "/users/:id"
}
Start the server:
json-server -p 3002 --watch db.json --routes routes.json
curl --request GET \
--url http://localhost:3002/api/v1/customers/1
result
{
"id": 1,
"name": "Ana"
}
The project has amazing documentation, it’s worth checking out.