In this task, we will set up a mock server API using json-server
. The API will serve the frontend with data for the posts app. We will create a db.json
file with some sample posts and run the mock server API using json-server
. We will also add API calls to fetch, create, update, and delete posts.
db.json
fileWe already have a sample db.json
file with some posts data in web/src/data
folder. You can create a new one or use the existing one. Bring this file to the api
directory.
This file will be used by json-server
to serve the posts data.
package.json
scriptsNext, create a package.json
file in the api
directory with the following content:
This file contains the start
script that runs json-server
with the db.json
file.
Navigate to the api
directory and run the following command:
You should see the following output:
You can now access the API homepage at http://localhost:3000
.
Visit http://localhost:3000/posts
to see the posts data.
In this step, we will add API calls to fetch, create, update, and delete posts. We will create a new file env.ts
in the web/src
directory to store the API URL. We will then create functions to fetch all posts, create a post, update a post, and delete a post.
Currently, the API server runs on http://localhost:3000
. However, this might change in the future. In particular, when deploying the app to production, the API URL will be different. To make it easier to update the API URL, we will store it in an environment variable. Create a .env
file in the web
directory with the following content:
Notice the use of VITE_
prefix for environment variables in Vite.
Next, create a new file env.ts
in the web/src
directory to access the environment variable and expose them as constants.
Notice the use of import.meta.env
to access the environment variables when using Vite.
Create a new file api.ts
in the web/src/data
directory and add the following code to fetch all posts:
Add the following code to delete a post by id:
Notice that we return true
if the deletion is successful. This is because json-server
does not return any data when deleting a resource. In a real-world scenario, the API would return the deleted resource.
Moreover, notice how we use { method: "DELETE" }
in the fetch
call to send a DELETE
request to the server.
Add the following code to create a new post:
Notice how we use { method: "POST" }
in the fetch
call to send a POST
request to the server. We also include the Content-Type
header with the value application/json
to indicate that we are sending JSON data in the request body.
The request body contains the content
of the post and the date
of the post, which is set to the current date and time using new Date().toISOString()
.
Add the following code to update a post by id:
Notice how we use { method: "PATCH" }
in the fetch
call to send a PATCH
request to the server. We also include the Content-Type
header with the value application/json
to indicate that we are sending JSON data in the request body.
The PATCH
request updates the content
of the post with the new content
value provided. The date
of the post remains unchanged. In some cases, you may want to use the PUT
method to replace the entire resource with the new data.
In this task, we set up a mock server API using json-server
to serve the frontend with data for the posts app. We created a db.json
file with sample posts data and ran the mock server API using json-server
. We also added API calls to fetch, create, update, and delete posts. We stored the API URL in an environment variable and created functions to interact with the API.