How to setup a mock ReSTful API in minutes…

Younes
Younes
Mar 7, 2019 · 2 min read

… using JSON Generator, JSON Server & Zeit’s Now.

During some of the trainings we deliver, we need some operational mock ReSTful API with CRUD operations support and filtering features.

Generate JSON data

JSON Generator is an original online tool that allows us to generate a bunch of resources using a specific but yet intuitive syntax.

Image for post
Image for post
JSON Generator

You can now download or copy the generate JSON array.

Setup the server

First, let’s create a NodeJS module!

mkdir api
cd api
yarn init # or npm init

JSON Server is an easy to setup ReSTful API mock server that uses a JSON file as a database for storing resources.

  1. Let’s install it.
yarn add json-server # or npm install --save json-server

2. Now, we can add the JSON array generated before to a db.json file with the following structure:

{
"users": [
{
"id": ...
},
...
]
}

3. Finally, we just have to add the start script in the package.json.

{
...
"scripts": {
"start": "json-server db.json"
}
...
}

The API should be running now and you can start querying it locally but you might want to deploy it somewhere in the cloud so you can share it with your friends or use it from Stackblitz or any other online code editor.

Deploy using Zeit’s Now

The good news is that thanks to Zeit’s Now, deploying the mock API will just require us to type one command: now.

Well, first you will have to sign up on Zeit (https://zeit.co/signup) and install now CLI using yarn global add now (or npm install -g now).

Now, we can go back to our API’s NodeJS module we created before and run the now command.

Image for post
Image for post
Zeit’s Now

Marmicode

At Marmicode, we use our passion and experience in Web…

Younes

Written by

Younes

Google Developer Expert for Angular & Web Technologies | eXtreme Programming Coach | Tech Advisor

Marmicode

Marmicode

At Marmicode, we use our passion and experience in Web Development & eXtreme Programming to help you cook better apps, ship them fast and make you proud of your work. #javascript #python #angular #continuousdeployment

Younes

Written by

Younes

Google Developer Expert for Angular & Web Technologies | eXtreme Programming Coach | Tech Advisor

Marmicode

Marmicode

At Marmicode, we use our passion and experience in Web Development & eXtreme Programming to help you cook better apps, ship them fast and make you proud of your work. #javascript #python #angular #continuousdeployment

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store