# Day 20 of 100days of code.

### Fundamentals of nodeJS

## Handling POST Request

We are going to implement a new router handler for the POST request using the same endpoint to add new movie on the list. In other to create a new movie, we need to attach a request body and to attach the request body to the request object we need to use a middleware. We use `app.use(express.json())` as the middleware and this middleware will add the request body to the request object.

A middleware is just a function that can modify the incoming request data.👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707238342857/e4fa4754-e663-4abb-9a80-357a3d015049.png align="center")

And when we send a post request we get this as a response👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707238387813/221f9e9e-2939-4bb2-851e-7cb8c8abdc0e.png align="center")

And also the req body on the console👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707238423968/8cd9a376-7e4e-4e45-9794-49440b465c50.png align="center")

But what we really want to achieve is that we want to update the `movies.json` file whenever we make a POST request. You noticed from the POST request screenshot, we did not add an ID to the req body because the ID is automatically generated. We need to figure out how to generate an ID for the new movie object. 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707238876475/7a23ca4a-4c35-4742-8d71-52613434f201.png align="center")

Now we need to create a new movie object. This new movie object will have the data which we are receiving in the request body plus the new ID we just created. And we are using the `writeFile()` method to update our `movies.json` file 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707239869167/7b274393-f7c3-4f01-899f-8b860aa4cd7d.png align="center")

We get the below response from the server, we successfully added a new movie with a unique ID to the movie list . 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707239840329/fbd50aa8-1add-4d0c-b820-20b69aa43f1a.png align="center")

And on our `movies.json` file, a new movie was also added to the list. 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707240124604/dedfc1db-1805-4a1a-ab48-3fcf0ce5be04.png align="center")

And when we request the movies list from the server using the GET request, we see that all the movies are listed including the new movie we just added to the list👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707240339540/ccf598bc-c586-417e-9654-4d6a0abe2c39.png align="center")

That's all for this lesson.

**Next**: *Handling Route Parameters*

**See you soon**😊🫡✌🏼...
