Let's get started with express routing.
Well, before we get into this, let's look at the final project that we are going to build.
We are going to build a simple API where we can perform all the operations like delete, create, update
and fetch a single record.
And we are not going to use any database yet, but instead we are going to use the FS module from Node.js
to write the data onto a file.
And I will also show you a tool called Postman for API Development.
So let's get back to the project and see how it's going to work.
The cool thing about this application is that as soon as I run the server, it is going to create a
folder called Data where we have a file called post dot JSON file.
So let's run the server and see if it's going to create those folders and files.
So put your eyes here.
As soon as the server get connected, you can see that it's going to generate a folder for me.
Moment of truth.
And indeed we have a folder called Data and inside it has generated a file called post dot json file.
And on that it has written a content or a sample post onto the file how awesome it is.
And you are going to create the function to get this done.
And that function is inside the utils file.
So now we have a data where we can work on.
So on this file we can add a new post onto this file or we can fetch a single record on this file or
we can delete a file and it will reflect.
So let's see how it's going to work.
Now back to my server file here.
Let me use the file that I created.
That is the JSON file.
So back to Postman.
What I'm going to do is that I'm going to fetch all the posts inside the JSON file.
So now moment of truth.
When I hit send it will see that the post that the server created and this is what I see here, check
it out.
And indeed this is what we have.
So what about if we want to add a record or post onto the file?
Now let's see.
And you are going to use what is called create and you are going to use HTTP method called post.
So now when I hit send, now have a look.
It will see that I get very full message that post created.
Now let's check it out on the file.
Indeed, I have a new post onto the file.
How awesome it is and I can go ahead and create new post this time around.
Let me see how to learn CSS like that and then this.
I'm going to be the you are going to be something like two and four description, let's say the best
post ever to when I hit send.
Now have a look on the file.
Have a look.
It could say that indeed we have it and has given me the ID automatically.
You can see.
And it's even added a new post on top of it because I use on shift meaning for on shift it adds a new
content onto the array.
So what about if you are trying to delete a post or a record?
Right now you have three real code 1 to 3.
So let's say I want delete the one wait id 202.
So now the end point or the root is going to be delete.
So I provide to zero two.
Now when I hit st indeed I have a message.
Is that post deleted successfully?
And you can see that we have two files now I can go ahead and then delete the rest.
Let's say I want to provide the ID of two and or one.
Let me say to let me hit send and you can see that post has been deleted.
So let me create more post here and let's check the file and you can see that now I have two files,
whatever.
If I want to fetch a single record, I'm going to use this endpoint.
So now I want to find a post with ID of one.
It means I'm going to retrieve this post.
So now have a look.
And you can see that this is a post I have.
What about if I want to update the same process I provide ID and let's say that this was going to be,
let me say, updated the title only hit send and now have a look.
And indeed I have one post being updated with a title of this.
I can go ahead and update the description.
Also, I will say that.
Great as that.
So let me hit send and now let's check it out.
And indeed it has been updated, guys.
So this is awesome project.