Skip to content

How to create an API with JSON-Server in 5 minutes

13 September, 2018
In this post you will learn how to CREATE A REST API with JSON-Server in 5 minutes, step by step, with CORS enabled and an API creation example.

In this post you will learn how to create a REST API with JSON-Server in 5 minutes, step by step, with CORS enabled. ENTERS!

 

WE START:

Welcome to another new post. When you are learning a JavaScript Framework (on the client side), such as Angujar JS, Ember, etc., generally you will need or want to make queries to an API that returns data that you can use, update, delete, etc, etc.

In general, everything that can be used an API. You could use a remote API, but of course, the most efficient thing is that you have a local REST API server, which returns data and you can use it there.

Today you will learn to provide yourself with a local server (which is called JSON-Server), which will do all the work for you to make a REST API available. We will detail it for testing purposes, but it will work the same as any other API you have in production. Without more to say, we begin.

How to create an API with JSON-Server in 5 minutes

In order to create an API with JSON-Server, perform the following steps:

1.- You must install Node JS (you can do it by entering its official page, and downloading the appropriate package for your operating system). The NPM package manager will be installed automatically during this process.

2.- Now, once Node JS is installed, you must install the JSON-Server package with NPM. Here is the command:

We recommend you:  Interesting documentary on the origins of Linux

npm install -g json-server

Be patient, as it may take a few seconds or minutes, that will depend on the network speed you have, and the number of packages needed to download.

3.- Next, you must create a JSON file that will contain the data that will be generated for this REST API. I leave you one useful tool to manage to intuitively create your JSON files.

As data, I leave you a JSON example of movies:

blank

4.- Okay, now, taking all this into account, let's create an API !:

json-server –watch movies.json

Through this command, we will be starting the server. We can create an API based on the JSON file that we have created as a data model.

With this you have already created a REST API. Access localhost: 3000 from your browser, where it will give you a screenshot of the result. Note that in the case of our movie JSON, we will have the links to access the data of that entity.

blank

Were you able to create a REST API successfully? Let us know in the comments!

If you like programming, In this blog we like coffee and writing these topics for you, programmer! Thanks for stopping by. See you in another post.