1
00:00:06,790 --> 00:00:07,930
Welcome back.

2
00:00:07,960 --> 00:00:14,470
In this video, let's see how we can implement post drought, how we can create a post.

3
00:00:14,710 --> 00:00:19,630
Well, for this drought, we need to think more.

4
00:00:20,020 --> 00:00:25,990
Like I said, if you are going to use MongoDB or database, what we are going to do will be pretty easy.

5
00:00:25,990 --> 00:00:30,910
But because we are going to use file system, you're going to have a couple of steps.

6
00:00:30,910 --> 00:00:33,130
So bear with me and let's continue.

7
00:00:33,520 --> 00:00:37,360
So let's make sure that the roots exists or it runs.

8
00:00:37,360 --> 00:00:45,280
So let's go ahead and make a request and the route exists and we have create push roots and which is

9
00:00:45,280 --> 00:00:48,280
this one, create push root.

10
00:00:48,430 --> 00:00:48,910
All right.

11
00:00:48,910 --> 00:00:50,950
So how can we create a post?

12
00:00:50,980 --> 00:00:58,960
The first step is we need to receive the data from the user, from postman, and we can do the same

13
00:00:58,960 --> 00:01:00,460
thing from a form.

14
00:01:00,460 --> 00:01:06,250
So we are going to use Postman as a way of using a form to send data to our server.

15
00:01:06,580 --> 00:01:10,210
So for this for Postman, how can we send a data?

16
00:01:10,210 --> 00:01:13,480
Please pay attention about this and this.

17
00:01:13,480 --> 00:01:16,660
How we can send data for these tasks.

18
00:01:16,660 --> 00:01:19,330
Click on body and on the body.

19
00:01:19,360 --> 00:01:21,460
Let me collapse this one.

20
00:01:21,460 --> 00:01:21,970
Good.

21
00:01:21,970 --> 00:01:31,480
So on the body we see this dropdown click on raw and under text, click on JSON and here we are going

22
00:01:31,480 --> 00:01:35,290
to provide the data we want to send as a JSON format.

23
00:01:35,290 --> 00:01:41,950
We are going to do the same thing when we are creating the folder by providing some dummy text here

24
00:01:41,950 --> 00:01:45,460
so we can copy this one and then paste it here.

25
00:01:45,460 --> 00:01:47,170
But let's move step by step.

26
00:01:47,170 --> 00:01:53,200
We provide object with key value pair, but they must be in double code.

27
00:01:53,230 --> 00:01:58,870
The first property here is called header because it is what we need to create a post.

28
00:01:59,140 --> 00:02:04,330
So let me call this one as Node.js, the title of the post I want to create.

29
00:02:04,480 --> 00:02:06,850
The next is the you are error.

30
00:02:07,390 --> 00:02:12,340
Don't worry, the order doesn't matter here, but the property names is very important here.

31
00:02:12,610 --> 00:02:16,600
So for the you are or let's make use of HTTP column.

32
00:02:17,390 --> 00:02:21,050
Voice like voice like some u r l dot com.

33
00:02:21,440 --> 00:02:28,970
And then the description is going to be any description of your choice you want to create.

34
00:02:28,970 --> 00:02:32,660
Let me say excellent post.

35
00:02:33,230 --> 00:02:38,300
All right, now we have the post or the payload you want to send.

36
00:02:38,420 --> 00:02:42,560
When I hit send, you can see that I still see details.

37
00:02:42,560 --> 00:02:51,380
And also on the post file here on the Jason, I don't see this payload or data, so how can we receive

38
00:02:51,380 --> 00:02:54,830
this payload inside our route?

39
00:02:54,830 --> 00:02:56,840
And here is a clue.

40
00:02:57,260 --> 00:03:05,270
We can receive this data inside our root on what is called rec dot body.

41
00:03:05,270 --> 00:03:10,130
So request we have a method called body.

42
00:03:10,130 --> 00:03:17,840
So this body contains all the properties and values we pass in into the server or into the root.

43
00:03:17,870 --> 00:03:25,130
So if you try to console that log, let's console log the rec dot body.

44
00:03:25,160 --> 00:03:29,510
Now when I hit send, let's check the console.

45
00:03:29,690 --> 00:03:35,180
Well, let me move this console log from the previous video, the post here.

46
00:03:35,360 --> 00:03:38,450
All right, so let's save it and let's make the request again.

47
00:03:38,840 --> 00:03:43,140
And now let's see that we have own define.

48
00:03:43,160 --> 00:03:47,360
Now here is a problem by default.

49
00:03:47,390 --> 00:03:52,550
Express does not pass any incoming data inside our root.

50
00:03:52,550 --> 00:04:01,040
So unless we tell Express that place express, please pass any data that is coming in to the root and

51
00:04:01,040 --> 00:04:02,930
add it to the record body.

52
00:04:02,930 --> 00:04:04,400
So this is how we're going to do.

53
00:04:04,910 --> 00:04:08,240
We are going to pass that one as a middleware.

54
00:04:08,240 --> 00:04:12,890
And also we have a specific section to talk about middleware.

55
00:04:12,920 --> 00:04:16,579
For the meantime, bear with me and type what I'm going to do.

56
00:04:16,579 --> 00:04:23,030
So let's say that pass incoming data so it is how we do it.

57
00:04:23,030 --> 00:04:29,270
We bring in the app and to plug in middleware, I'm going to use use and then you pass in the module.

58
00:04:29,630 --> 00:04:35,420
And middleware simply means is a function that have access to req and then the response, don't worry,

59
00:04:35,420 --> 00:04:37,310
we'll talk more about media world.

60
00:04:37,430 --> 00:04:43,010
So we're going to use express middleware and here we go, express dot JSON.

61
00:04:43,010 --> 00:04:45,860
And that is all and that is all.

62
00:04:45,860 --> 00:04:50,990
Well, with the current version of Express, there is no need to install a third party package called

63
00:04:50,990 --> 00:04:51,920
Body Parser.

64
00:04:51,980 --> 00:04:54,290
So we have this one by default.

65
00:04:54,290 --> 00:04:58,850
So now that we have this one, save it and now let's make a request again.

66
00:04:58,850 --> 00:05:01,100
Send and now back to terminal.

67
00:05:01,130 --> 00:05:06,440
And now we have something incredible, which is a data I pass in.

68
00:05:06,440 --> 00:05:13,670
So now that I'm receiving the data inside my route, how can I add to this array?

69
00:05:13,700 --> 00:05:17,000
So here we are going to make use of JavaScript.

70
00:05:17,120 --> 00:05:18,680
So here we go.

71
00:05:18,980 --> 00:05:24,050
Now let's remove this one from here and this one from here.

72
00:05:24,050 --> 00:05:27,170
Now we know the body is the post we want to create.

73
00:05:27,350 --> 00:05:33,770
So let's go ahead and get the post, say new post, and then it's equal to rec.

74
00:05:34,960 --> 00:05:36,190
Dot body.

75
00:05:36,580 --> 00:05:38,200
You saw it in the console.

76
00:05:38,770 --> 00:05:46,300
Next ace, we are going to push this new post into the array of posts.

77
00:05:46,540 --> 00:05:49,930
So next step is let me have a beautiful comment here.

78
00:05:49,960 --> 00:05:56,200
Step one is get the post from user.

79
00:05:56,620 --> 00:05:59,560
The next step is push.

80
00:06:00,380 --> 00:06:06,830
The new post into the existing post.

81
00:06:06,860 --> 00:06:10,140
Existing post, that's the post data.

82
00:06:10,160 --> 00:06:11,190
So here we go.

83
00:06:11,270 --> 00:06:15,500
We will take the post data from the file, which is this one.

84
00:06:15,560 --> 00:06:20,570
And on that, we're going to use push to push the data into the array.

85
00:06:20,660 --> 00:06:22,280
And what do you want to push?

86
00:06:22,310 --> 00:06:26,360
You want to push the new post as that.

87
00:06:26,750 --> 00:06:27,580
As that.

88
00:06:27,590 --> 00:06:31,850
But we want to add an ID to weight dynamically.

89
00:06:31,850 --> 00:06:37,880
So here, because it's an object, we can add additional property to the push.

90
00:06:38,270 --> 00:06:44,390
So here, let's provide us an object and then let's pray it all the new posts.

91
00:06:44,420 --> 00:06:47,810
And next is you want to add our own ID into it.

92
00:06:47,960 --> 00:06:53,120
And for the ID, we can use math at random to get random numbers.

93
00:06:53,120 --> 00:06:56,990
But let's make it pretty simple by making use of the post data.

94
00:06:58,540 --> 00:07:00,840
Post data dot length.

95
00:07:00,850 --> 00:07:02,800
And then here we go.

96
00:07:02,800 --> 00:07:08,560
And you want to convert everything to string because what you get back from that is a number.

97
00:07:08,560 --> 00:07:11,650
So let's convert everything into a string as that.

98
00:07:11,650 --> 00:07:13,360
So now here we go.

99
00:07:13,360 --> 00:07:20,410
So let's go ahead and console.log the new updated post or post data, because at this point we have

100
00:07:20,410 --> 00:07:23,710
added a new record into it.

101
00:07:23,920 --> 00:07:28,660
Let's save it and make a request and see what is going to happen.

102
00:07:28,930 --> 00:07:30,690
Now I'm back to my terminal.

103
00:07:30,730 --> 00:07:33,250
I see this console log.

104
00:07:33,280 --> 00:07:35,950
Well, let's check the code again.

105
00:07:36,430 --> 00:07:37,690
Well, sorry, guys.

106
00:07:37,690 --> 00:07:45,190
It's supposed to be inside the root, so let me cut it and then paste it before the push method.

107
00:07:45,400 --> 00:07:51,490
So now by default, we have only one post inside the array, which is this.

108
00:07:51,700 --> 00:07:55,300
So let's go ahead and make the request and check the terminal.

109
00:07:55,300 --> 00:08:03,460
And you can see that we have to record the new one that we added from Postman, which is this one.

110
00:08:03,700 --> 00:08:10,660
But if we check on the post adjacent file, we don't see it because we need to write the content onto

111
00:08:10,660 --> 00:08:11,410
the file.

112
00:08:11,740 --> 00:08:14,230
So now back to the server file here.

113
00:08:14,230 --> 00:08:21,130
So after pushing the data that we are creating into the existing one, what we are going to do is you

114
00:08:21,130 --> 00:08:23,380
want to write onto the file.

115
00:08:23,530 --> 00:08:25,270
So here we go.

116
00:08:25,570 --> 00:08:28,300
Let's give it a beautiful command says.

117
00:08:28,780 --> 00:08:29,890
Right.

118
00:08:30,880 --> 00:08:31,990
So we file.

119
00:08:34,080 --> 00:08:39,870
And we are going to use the FFS module, so let's require the package here.

120
00:08:39,900 --> 00:08:46,470
Costs f s is equal to required the f.

121
00:08:46,470 --> 00:08:56,850
S module, save it and now back into our create push route, which is this on the F s we have a method

122
00:08:56,850 --> 00:09:04,920
call right file and for this one we have covered in details under no co modules.

123
00:09:05,250 --> 00:09:13,680
The first element is the path or the file that we want to create and the file is inside here data and

124
00:09:14,040 --> 00:09:15,750
post JSON file.

125
00:09:15,750 --> 00:09:16,890
So here we go.

126
00:09:16,890 --> 00:09:22,740
It's going to be data for S slash post dot JSON file.

127
00:09:22,950 --> 00:09:30,210
And the next agreement is a content that you want to write, which is this post data.

128
00:09:30,210 --> 00:09:34,110
And the next segment is going to be a callback function.

129
00:09:34,110 --> 00:09:39,270
Let's use normal function here and we have access to the error only.

130
00:09:39,270 --> 00:09:42,690
So let's check if there is an error.

131
00:09:42,690 --> 00:09:43,620
Why not?

132
00:09:43,620 --> 00:09:47,760
We go ahead and then console.log the error.

133
00:09:48,180 --> 00:09:57,610
Then lastly down here we are going to send a message, send message to the element.

134
00:09:59,680 --> 00:10:01,030
Message.

135
00:10:02,100 --> 00:10:04,730
To the user or the client.

136
00:10:04,740 --> 00:10:17,250
So here we go red dot Jason and you pass an object with a message says that post created.

137
00:10:20,650 --> 00:10:22,630
Successfully.

138
00:10:23,470 --> 00:10:29,770
All right, then let me send the data that I created, which is the new post, or let's send a message

139
00:10:29,770 --> 00:10:30,520
like this.

140
00:10:30,550 --> 00:10:31,150
All right.

141
00:10:31,390 --> 00:10:33,730
So now moments of truth.

142
00:10:34,300 --> 00:10:36,320
So back to Postman.

143
00:10:36,340 --> 00:10:41,080
Let's make the request and I get some beautiful error.

144
00:10:41,110 --> 00:10:44,260
Say that invalid agreement type.

145
00:10:44,560 --> 00:10:51,070
Meaning that the method for right file takes in a string.

146
00:10:51,580 --> 00:10:55,060
But we have passed in an array.

147
00:10:55,090 --> 00:10:57,040
That's why we have that error.

148
00:10:57,280 --> 00:11:01,450
We are going to use a method called Jason or string or file.

149
00:11:01,480 --> 00:11:06,610
So let me cut this one and bring in Jason uppercase.

150
00:11:07,660 --> 00:11:14,230
Just sayin dart string ify and then pass in the post data and that is it.

151
00:11:14,230 --> 00:11:20,650
So now our server has restarted down here and now let's make the request again.

152
00:11:20,650 --> 00:11:24,280
And now if we see that we get a message that post created.

153
00:11:24,340 --> 00:11:29,620
Now if you check on the file, you see that the post has been created down here.

154
00:11:29,680 --> 00:11:32,350
So now I can make a request.

155
00:11:32,380 --> 00:11:36,340
Let me click here to expand and make a request to fetch all.

156
00:11:36,340 --> 00:11:40,390
And now I have to post on the file.

157
00:11:40,570 --> 00:11:47,980
You can see that the newly created are added at the end of the array, but I want to bring this one

158
00:11:47,980 --> 00:11:48,760
at the top.

159
00:11:48,970 --> 00:11:52,030
So you're going to use a method called on shift.

160
00:11:52,030 --> 00:11:54,640
So on shift instead of push.

161
00:11:54,640 --> 00:12:00,890
So let's make a request, create new posts, let's say Node.js two and this one, two, let's add two,

162
00:12:00,910 --> 00:12:01,420
two to it.

163
00:12:01,450 --> 00:12:02,440
Now post created.

164
00:12:02,470 --> 00:12:04,210
Now let's check the file.

165
00:12:04,210 --> 00:12:10,420
And now we see that now we have this one being created as that that is perfect.

166
00:12:10,450 --> 00:12:16,060
Now this how we're able to send a post request and then save it onto the file.

167
00:12:16,300 --> 00:12:17,410
The next video.

168
00:12:17,440 --> 00:12:21,610
Let's work on how we can implement delete functionality.

