Welcome back.
In this video, let's see how we can implement post drought, how we can create a post.
Well, for this drought, we need to think more.
Like I said, if you are going to use MongoDB or database, what we are going to do will be pretty easy.
But because we are going to use file system, you're going to have a couple of steps.
So bear with me and let's continue.
So let's make sure that the roots exists or it runs.
So let's go ahead and make a request and the route exists and we have create push roots and which is
this one, create push root.
All right.
So how can we create a post?
The first step is we need to receive the data from the user, from postman, and we can do the same
thing from a form.
So we are going to use Postman as a way of using a form to send data to our server.
So for this for Postman, how can we send a data?
Please pay attention about this and this.
How we can send data for these tasks.
Click on body and on the body.
Let me collapse this one.
Good.
So on the body we see this dropdown click on raw and under text, click on JSON and here we are going
to provide the data we want to send as a JSON format.
We are going to do the same thing when we are creating the folder by providing some dummy text here
so we can copy this one and then paste it here.
But let's move step by step.
We provide object with key value pair, but they must be in double code.
The first property here is called header because it is what we need to create a post.
So let me call this one as Node.js, the title of the post I want to create.
The next is the you are error.
Don't worry, the order doesn't matter here, but the property names is very important here.
So for the you are or let's make use of HTTP column.
Voice like voice like some u r l dot com.
And then the description is going to be any description of your choice you want to create.
Let me say excellent post.
All right, now we have the post or the payload you want to send.
When I hit send, you can see that I still see details.
And also on the post file here on the Jason, I don't see this payload or data, so how can we receive
this payload inside our route?
And here is a clue.
We can receive this data inside our root on what is called rec dot body.
So request we have a method called body.
So this body contains all the properties and values we pass in into the server or into the root.
So if you try to console that log, let's console log the rec dot body.
Now when I hit send, let's check the console.
Well, let me move this console log from the previous video, the post here.
All right, so let's save it and let's make the request again.
And now let's see that we have own define.
Now here is a problem by default.
Express does not pass any incoming data inside our root.
So unless we tell Express that place express, please pass any data that is coming in to the root and
add it to the record body.
So this is how we're going to do.
We are going to pass that one as a middleware.
And also we have a specific section to talk about middleware.
For the meantime, bear with me and type what I'm going to do.
So let's say that pass incoming data so it is how we do it.
We bring in the app and to plug in middleware, I'm going to use use and then you pass in the module.
And middleware simply means is a function that have access to req and then the response, don't worry,
we'll talk more about media world.
So we're going to use express middleware and here we go, express dot JSON.
And that is all and that is all.
Well, with the current version of Express, there is no need to install a third party package called
Body Parser.
So we have this one by default.
So now that we have this one, save it and now let's make a request again.
Send and now back to terminal.
And now we have something incredible, which is a data I pass in.
So now that I'm receiving the data inside my route, how can I add to this array?
So here we are going to make use of JavaScript.
So here we go.
Now let's remove this one from here and this one from here.
Now we know the body is the post we want to create.
So let's go ahead and get the post, say new post, and then it's equal to rec.
Dot body.
You saw it in the console.
Next ace, we are going to push this new post into the array of posts.
So next step is let me have a beautiful comment here.
Step one is get the post from user.
The next step is push.
The new post into the existing post.
Existing post, that's the post data.
So here we go.
We will take the post data from the file, which is this one.
And on that, we're going to use push to push the data into the array.
And what do you want to push?
You want to push the new post as that.
As that.
But we want to add an ID to weight dynamically.
So here, because it's an object, we can add additional property to the push.
So here, let's provide us an object and then let's pray it all the new posts.
And next is you want to add our own ID into it.
And for the ID, we can use math at random to get random numbers.
But let's make it pretty simple by making use of the post data.
Post data dot length.
And then here we go.
And you want to convert everything to string because what you get back from that is a number.
So let's convert everything into a string as that.
So now here we go.
So let's go ahead and console.log the new updated post or post data, because at this point we have
added a new record into it.
Let's save it and make a request and see what is going to happen.
Now I'm back to my terminal.
I see this console log.
Well, let's check the code again.
Well, sorry, guys.
It's supposed to be inside the root, so let me cut it and then paste it before the push method.
So now by default, we have only one post inside the array, which is this.
So let's go ahead and make the request and check the terminal.
And you can see that we have to record the new one that we added from Postman, which is this one.
But if we check on the post adjacent file, we don't see it because we need to write the content onto
the file.
So now back to the server file here.
So after pushing the data that we are creating into the existing one, what we are going to do is you
want to write onto the file.
So here we go.
Let's give it a beautiful command says.
Right.
So we file.
And we are going to use the FFS module, so let's require the package here.
Costs f s is equal to required the f.
S module, save it and now back into our create push route, which is this on the F s we have a method
call right file and for this one we have covered in details under no co modules.
The first element is the path or the file that we want to create and the file is inside here data and
post JSON file.
So here we go.
It's going to be data for S slash post dot JSON file.
And the next agreement is a content that you want to write, which is this post data.
And the next segment is going to be a callback function.
Let's use normal function here and we have access to the error only.
So let's check if there is an error.
Why not?
We go ahead and then console.log the error.
Then lastly down here we are going to send a message, send message to the element.
Message.
To the user or the client.
So here we go red dot Jason and you pass an object with a message says that post created.
Successfully.
All right, then let me send the data that I created, which is the new post, or let's send a message
like this.
All right.
So now moments of truth.
So back to Postman.
Let's make the request and I get some beautiful error.
Say that invalid agreement type.
Meaning that the method for right file takes in a string.
But we have passed in an array.
That's why we have that error.
We are going to use a method called Jason or string or file.
So let me cut this one and bring in Jason uppercase.
Just sayin dart string ify and then pass in the post data and that is it.
So now our server has restarted down here and now let's make the request again.
And now if we see that we get a message that post created.
Now if you check on the file, you see that the post has been created down here.
So now I can make a request.
Let me click here to expand and make a request to fetch all.
And now I have to post on the file.
You can see that the newly created are added at the end of the array, but I want to bring this one
at the top.
So you're going to use a method called on shift.
So on shift instead of push.
So let's make a request, create new posts, let's say Node.js two and this one, two, let's add two,
two to it.
Now post created.
Now let's check the file.
And now we see that now we have this one being created as that that is perfect.
Now this how we're able to send a post request and then save it onto the file.
The next video.
Let's work on how we can implement delete functionality.