Setting Up a Mock REST API Server for Frontend Development
When developing the front end of an application, waiting for the back end to be ready can sometimes slow down the development process. This is especially true when the front end requires specific data structures or endpoints to function correctly. A solution? Mock the REST API! This not only allows frontend development to proceed independently but also serves as a specification for backend developers. In this post, we'll explore how we set up a mock REST API server using json-server
and deployed it on Render.com.
The Challenge:
The main goal was to create a mock REST API server that could mimic the expected responses from a real backend. The server needed to be flexible, allowing updates to the mock data as frontend requirements evolved. Additionally, it was crucial to deploy the mock server online so that it could be shared with both frontend and backend teams.
The Solution: json-server
and Render.com:
Choosing
json-server
:We opted for
json-server
, a zero-coding tool that can generate a full mock REST API based on a simple JSON file. It supports various HTTP methods likeGET
,POST
,PUT
,PATCH
, andDELETE
, making it versatile for mimicking a real RESTful API.Structuring the Mock Data:
The mock data was structured in a
db.json
file. Initially, there were challenges in structuring nested routes, but withjson-server
's route rewrites, we could mimic almost any API structure.Deploying on Render.com:
Render.com was chosen for deployment because of its simplicity and integration with Git repositories. The steps included:
Setting up a Node.js environment.
Configuring the
package.json
to startjson-server
on the correct port and host.Pushing the repository to a Git platform.
Deploying on Render.com using the repository.
Benefits Achieved:
Independent Frontend Development:
With the mock server in place, front-end development could proceed without waiting for the backend. This led to faster development cycles and early identification of potential UI/UX issues.
Clear Backend Specifications:
The mock server served as a clear specification for the backend team. By sharing the mock API's endpoints and expected responses, there was less room for misunderstandings or mismatches in the data format.
Easy Updates:
As frontend requirements evolved, updates to the mock data were easy. This ensured that the mock server always reflected the most recent data needs.
Conclusion:
Setting up a mock REST API server proved invaluable in streamlining the development process. It allowed the frontend to be developed and tested independently, served as a clear specification for the backend, and provided a platform for easy updates. Tools like json-server
and platforms like Render.com make the process straightforward, even for developers with minimal backend experience. For teams looking to optimize their frontend-backend workflow, a mock server is definitely worth considering!
Subscribe to my newsletter
Read articles from Mustafa Dalga directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mustafa Dalga
Mustafa Dalga
I am a highly qualified software developer with over 6 years of experience in developing desktop and web applications using various technologies.My main programming language is JavaScript, and I am currently working as a Front-end developer at Heybooster. I am passionate about software development and constantly seek to improve my skills and knowledge through research and self-teaching. I share my projects on Github as open source to not only learn and grow, but also contribute to the development community. As a software developer, I am committed to upholding ethical standards in my work.