Creating a Fake REST API and Deploy
The importance of API in modern applications is a known fact nowadays. APIs help to build an application so efficient and secure as well. The Integration with other third-party components became so easier now because of APIs. APIs are developed by following majorly three types of architecture or protocols.
Now, coming back to the main agenda behind this article. As we often see, we may need some dummy or fake APIs to ensure the backend functionalities of the applications, either in its Development phase or in its Testing phase. Sometimes the actual API which supposes to integrate with the application may not be ready for testing and which may be a cause of blockers. To overcome such kinds of situations we can develop our own fake API and deploy it. So, that it can be easy to use by anyone from anywhere.
In this article, I will demonstrate the steps to create a Fake REST API using json-server on local first and then deploy it on the Heroku cloud platform. So, let’s get started 🏃♀️
For ease of understanding, I am dividing this entire discussion into two parts.
- Develop your own API
- Deploy your API
Develop your own API
Before starting with actual development we need to make sure that we have the below dependencies available in our project as well as the system.
- Check the status of Node.JS: If you already have installed node.js in your system then it is fine or else please download and install node.js first from here.
- Also, ensure that
npm
has been installed properly in your system by checking its version. Executenpm -version
on command prompt or terminal - Once this is done, create your
package.json
and add the required dependencies as shown below.
For the entire project reference please clone the source code from here and run npm install
to install and configure the project.
Create `db.json` file
db.json
is the major file that will contain the actual data on which we can perform the basic API operations. i.e. GET, POST, PUT, PATCH, DELETE, and OPTIONS. In terms of creating this file, we will take the help of another two libraries which can generate some fake data for our API. faker and lodash.
The usage of faker
and lodash
can be found under generate.js
file
Once execute this file with json-server generateData.js
the command, it will generate 100
records for our API. A sample output provided below.
By default json-server
is running on http://localhost:3000
now. So you can check the output on the browser itself.
So, now simply copy the entire records and paste it on db.json
file and your data are ready now to access through API.
Now, if you executejson-server db.json
then json-server
will consider db.json
file as the backend and you can access https://localhost:3000/users
through any API clients (like Postman) and all other operations can be performed on it.
Create `server.js`
server.js
is responsible to start json-server
on the default port programmatically.
So now you have created your own REST API which is up and running on your local 👨🔧
Deploy your API
Now, it's time to make your API public. To do this you need to deploy your API on some server or cloud platform. Here we are going to use Heroku for deployment.
- If you do not have any existing account on Heroku then do the sign-up first from here.
- You also need to create a repository for your project on GitHub preferably
Once these steps are done you are ready for further steps of deployment.
- Download Heroku Command Line Interface (CLI) and install it
- Then open command prompt or terminal and execute
heroku login
. You will see Heroku login page will automatically open on your default browser post execute this command. - Once login is done, clone your repository in your local by executing the below command and navigate into the project folder
git clone <repository url>
cd <project folder path>
4. Execute heroku create
the command to create an heroku app
which will be a reference for your API project
5. Now, execute git push heroku main
to complete the deployment process
6. Build succeeded!
the message will appear on the console if it is successful.
7. To open your Heroku app from the command prompt or terminal run heroku open
command and you should see the JSON Server
page along with your API info.
- There is a very good step-by-step guide available for deployment here. Please refer to these steps for more clarity.
Note: After deploying the API you might get some errors while launching the Heroku app. Some common steps are provided below which may resolve the issue.
- Under the
Settings
tab of your Heroku app, specify theport
number inconfig vars
section, like below.
- Create
Procfile
under the root project folder, which will contain thenode
command which needs to be executed to trigger yourserver.js
file.
That’s all!
Endnote
First of all Thanks for reading this article, if you find this article useful then please hit the clap 👏 icon and also share with others. It will motivate me to write some more blogs on this kind of content.