1
00:00:06,370 --> 00:00:16,870
In this video, we are going to learn how we can pass a data to our server because a server can also

2
00:00:16,870 --> 00:00:23,320
take a data from a user process it and gives back the response.

3
00:00:23,770 --> 00:00:32,320
But to make it pretty easy, we are just going to learn how not just server can pass incoming data and

4
00:00:32,320 --> 00:00:34,720
then display it to the console.

5
00:00:34,900 --> 00:00:40,240
Remember, a server can take in a data and then send back the response.

6
00:00:40,240 --> 00:00:41,230
So let's see.

7
00:00:41,410 --> 00:00:48,280
So what you are going to do is that we are going to mimic in such a way that we are going to create

8
00:00:48,280 --> 00:00:51,970
a post with a title and then description.

9
00:00:51,970 --> 00:00:56,110
And with that data we want to receive it on our server.

10
00:00:56,110 --> 00:00:57,550
So let's see how.

11
00:00:57,550 --> 00:01:03,700
So let's collapse this once the one that we done with it and now let's open the server file.

12
00:01:04,000 --> 00:01:09,310
So now definitely we are also going to listen to an end point.

13
00:01:09,400 --> 00:01:12,340
So come down here after register.

14
00:01:12,490 --> 00:01:15,370
You see that pass?

15
00:01:16,410 --> 00:01:17,550
Incoming.

16
00:01:18,750 --> 00:01:19,500
Data.

17
00:01:20,190 --> 00:01:27,180
And we often say payload, meaning the data that a user is trying to send to our server.

18
00:01:27,510 --> 00:01:32,880
So here we are going to check for a post route or post endpoint.

19
00:01:33,390 --> 00:01:37,560
So the next step is how can we get the data?

20
00:01:37,680 --> 00:01:45,060
The first step is let's check the you are the user is trying to hit for our case.

21
00:01:45,060 --> 00:01:48,600
Let's say create post is going to be that.

22
00:01:48,930 --> 00:01:58,860
And guys, we have a couple of ways that we can send data to a server and then also get data from a

23
00:01:58,860 --> 00:01:59,500
server.

24
00:01:59,520 --> 00:02:03,030
And this is what we call a HTTP method.

25
00:02:03,180 --> 00:02:07,060
We have dedicated video and HTTP methods.

26
00:02:07,080 --> 00:02:15,810
But for now, any time you want to send data to a server, we use HTTP method called post.

27
00:02:15,990 --> 00:02:21,190
So on the request, we have all the HTTP method.

28
00:02:21,210 --> 00:02:26,640
For the meantime, let me comment this one out and let me show you what I mean here.

29
00:02:26,850 --> 00:02:35,940
So inside the home root here or any of this root or endpoint, let's try to console log the request,

30
00:02:35,940 --> 00:02:37,080
this request.

31
00:02:37,080 --> 00:02:43,950
But last we can do it here on the request we have for this call the method.

32
00:02:43,950 --> 00:02:45,750
So request that method.

33
00:02:45,780 --> 00:02:52,800
Now when I try to visit any of these endpoints, this console log will be triggered.

34
00:02:52,800 --> 00:02:53,700
So let's see.

35
00:02:53,940 --> 00:02:56,940
So let's restart the server.

36
00:02:58,830 --> 00:03:02,000
And Clay the console and here we go.

37
00:03:02,010 --> 00:03:04,560
So now let's go to any end points.

38
00:03:04,590 --> 00:03:05,160
Check it out.

39
00:03:05,160 --> 00:03:11,670
In the console, you can see that we have this HTTP method called get meaning.

40
00:03:11,670 --> 00:03:19,620
We are getting something from the server and if we want to send something to the server, we are going

41
00:03:19,620 --> 00:03:22,860
to use what is called post method.

42
00:03:23,100 --> 00:03:30,630
So now let's remove this one or I can leave it for now and now let's bring back this code.

43
00:03:31,200 --> 00:03:41,880
The first step is we are checking if the you are o is equal to create post and the request dot method.

44
00:03:43,270 --> 00:03:46,780
Is equal to post.

45
00:03:46,870 --> 00:03:48,340
Uppercase post.

46
00:03:48,820 --> 00:03:49,930
Then.

47
00:03:50,750 --> 00:03:55,550
It means that a user is trying to send something to us.

48
00:03:55,820 --> 00:03:59,450
So here let's receive the incoming data.

49
00:03:59,450 --> 00:04:02,060
So here receive.

50
00:04:03,460 --> 00:04:07,510
Receive the incoming.

51
00:04:08,710 --> 00:04:09,330
Data.

52
00:04:09,340 --> 00:04:10,150
Perfect.

53
00:04:10,150 --> 00:04:19,410
So let's assign to a variable as post and let's give it an empty array.

54
00:04:19,420 --> 00:04:21,730
This is how we pass data.

55
00:04:22,910 --> 00:04:23,540
Insight.

56
00:04:23,840 --> 00:04:32,090
Chase The next step is that we are going to listen on an event called Data.

57
00:04:32,120 --> 00:04:37,250
So on the request, remember, which is from this one.

58
00:04:37,980 --> 00:04:39,000
This one.

59
00:04:39,150 --> 00:04:43,200
We also have an event listener dot on.

60
00:04:44,020 --> 00:04:51,970
So you're going to listen to an event which is of type data, meaning if a user is trying to send us

61
00:04:51,970 --> 00:04:55,090
data, this event is going to be fired.

62
00:04:55,480 --> 00:04:57,940
So let's pass in callback function here.

63
00:04:57,940 --> 00:04:59,590
That is a function.

64
00:05:00,100 --> 00:05:05,500
And inside this function we have what is called the chunk.

65
00:05:05,500 --> 00:05:13,450
When we say chunk, it simply means that when we send a data to our server, it comes in piece by piece.

66
00:05:13,600 --> 00:05:17,930
Then we will compile it and then push it inside this post array.

67
00:05:17,950 --> 00:05:19,630
As you see over here.

68
00:05:19,960 --> 00:05:26,170
So inside here we have the actual payload as chunk.

69
00:05:26,530 --> 00:05:30,510
So here, if you try to console log chunk.

70
00:05:30,520 --> 00:05:31,930
Now let's see.

71
00:05:32,290 --> 00:05:37,590
So the chunk represents the pieces of data that we are receiving from the user.

72
00:05:37,600 --> 00:05:40,560
So now how can we make post request?

73
00:05:40,570 --> 00:05:42,290
How can we create?

74
00:05:42,310 --> 00:05:48,190
Well, we are going to use different tools to get it done because for backend development, we don't

75
00:05:48,190 --> 00:05:53,790
have a browser to enter data like maybe the title of the post the description.

76
00:05:53,800 --> 00:05:55,900
So we need a two.

77
00:05:56,050 --> 00:05:59,380
So we are going to use what is called tender client.

78
00:05:59,380 --> 00:06:07,520
And this is an extension that applies to make a request, whether post or get or PWD or whatever.

79
00:06:07,540 --> 00:06:10,570
Like I said, we have a video on this.

80
00:06:10,570 --> 00:06:14,800
For the meantime, let's see how we can send request to a server.

81
00:06:14,980 --> 00:06:25,480
So if you don't have tender client set for tender client hit enter and this is the extension tender

82
00:06:25,480 --> 00:06:26,490
client.

83
00:06:26,500 --> 00:06:31,090
So install it after installation, you're going to see something like this.

84
00:06:31,300 --> 00:06:33,490
So now here you go, open.

85
00:06:33,490 --> 00:06:36,430
That next step is click on new requests.

86
00:06:36,760 --> 00:06:38,520
The request has opened here.

87
00:06:38,530 --> 00:06:42,190
For the meantime, don't worry about what is going on here.

88
00:06:42,400 --> 00:06:45,450
The first step is because we want to make post request.

89
00:06:45,460 --> 00:06:48,310
Let's change this one to post again.

90
00:06:48,310 --> 00:06:50,800
Don't worry if you're not okay with this.

91
00:06:50,800 --> 00:06:57,430
We have a section talk about the two and we are going to use what is called postman instead of this

92
00:06:57,430 --> 00:06:57,760
one.

93
00:06:57,760 --> 00:07:03,430
So I just want to show you how we can receive data inside our server.

94
00:07:03,820 --> 00:07:14,020
So here you provide the you are going to be at HTTP semicolon, first left slash look host semicolon

95
00:07:14,020 --> 00:07:23,410
9004 slash create post you saw it as that and to be able to send data in the past in what is called

96
00:07:23,410 --> 00:07:23,870
body.

97
00:07:23,890 --> 00:07:30,310
Click on body here and inside the JSON content, I'm going to send what JSON data?

98
00:07:30,310 --> 00:07:36,130
Remember, we always say JSON data to a server for this.

99
00:07:36,130 --> 00:07:37,720
We can send any data here.

100
00:07:37,720 --> 00:07:46,930
So let's say title are going to be the property name and let's say best post and comma and description

101
00:07:47,230 --> 00:07:52,490
is that let's say that great post ever.

102
00:07:52,510 --> 00:07:58,540
Now when I hit send, you can see that something is happening here.

103
00:07:58,570 --> 00:08:03,580
Don't worry about this one because we are not sending anything to the user.

104
00:08:03,940 --> 00:08:05,730
So let's check the console.

105
00:08:05,740 --> 00:08:15,820
If I going to receive the data, which is the data that we sent and we are seeing nothing here is because

106
00:08:15,820 --> 00:08:20,920
we also need to listen to an event called End.

107
00:08:21,310 --> 00:08:29,530
So here to be able to receive the actual data that is from this on here we are going to change another

108
00:08:29,530 --> 00:08:34,090
event listener call on and the type is what you call.

109
00:08:34,090 --> 00:08:43,570
And at this point the process in the data has been passed from chunk to extra data and we have access

110
00:08:43,570 --> 00:08:45,430
inside this end.

111
00:08:45,430 --> 00:08:50,650
Event Listener So let's pass in normal function here it can be error function.

112
00:08:50,650 --> 00:08:58,480
And here if we try to console log let's say that anything data.

113
00:08:59,430 --> 00:09:00,120
Here.

114
00:09:00,480 --> 00:09:04,090
Okay, so now let's restart the server.

115
00:09:04,110 --> 00:09:06,720
Click the console, and here we go.

116
00:09:06,990 --> 00:09:13,830
And you could see that connection was forcibly closed by a player, meaning nothing sent to the user.

117
00:09:13,830 --> 00:09:14,460
So.

118
00:09:14,460 --> 00:09:16,680
So let's try it to send again.

119
00:09:17,070 --> 00:09:18,050
Hit send.

120
00:09:18,060 --> 00:09:19,710
Now check the console.

121
00:09:19,710 --> 00:09:30,510
And now you can see that we have data here and the chunk we see something like bar file and this is

122
00:09:30,510 --> 00:09:33,870
how Node.js parses incoming data.

123
00:09:34,320 --> 00:09:41,250
So a buffer simply means that the data is being passed bit by bit.

124
00:09:41,250 --> 00:09:47,040
So after everything is complete, then convert bar to actual data.

125
00:09:47,430 --> 00:09:56,280
So here what we are going to do is that now our survival hang up because inside here, inside here,

126
00:09:56,640 --> 00:10:03,210
this function call here we are not sending anything to the user or we are ending the the response.

127
00:10:03,210 --> 00:10:05,430
That's why the server is still hanging.

128
00:10:05,820 --> 00:10:14,310
So now inside here we are going to push the post dot push and now you push in the chunk.

129
00:10:14,790 --> 00:10:21,150
So inside the end here we are going to pass the buffer to actual data.

130
00:10:21,150 --> 00:10:25,950
So here pass the.

131
00:10:27,660 --> 00:10:33,180
Bar from data into string or actual data.

132
00:10:33,570 --> 00:10:34,970
Okay, so here we go.

133
00:10:34,980 --> 00:10:37,440
So Konst passed.

134
00:10:39,070 --> 00:10:44,320
Data is equal to the buffer or is a method on that.

135
00:10:44,320 --> 00:10:53,950
We have what is called contact and then we can cut what we can cut the post which is here and you pass

136
00:10:53,950 --> 00:10:54,790
passing here.

137
00:10:54,850 --> 00:11:00,580
And lastly, we need to convert to string and that is it.

138
00:11:01,270 --> 00:11:06,250
The next step is we need to send the data to the user.

139
00:11:06,250 --> 00:11:06,820
Right?

140
00:11:06,820 --> 00:11:13,360
So let's send the header rest dot right head.

141
00:11:16,000 --> 00:11:19,000
Right head, then it passed in 200.

142
00:11:19,000 --> 00:11:20,140
That is okay.

143
00:11:20,290 --> 00:11:23,590
And now the content type for this one.

144
00:11:23,600 --> 00:11:26,260
Should we pass in JSON data or object?

145
00:11:27,010 --> 00:11:29,080
So here content.

146
00:11:30,700 --> 00:11:31,600
Type.

147
00:11:33,140 --> 00:11:33,590
Again.

148
00:11:33,590 --> 00:11:34,670
Let's just send.

149
00:11:36,190 --> 00:11:40,450
A type of atmo or adjacent.

150
00:11:41,390 --> 00:11:42,140
Don't worry.

151
00:11:42,170 --> 00:11:42,920
For now.

152
00:11:43,910 --> 00:11:44,520
Okay.

153
00:11:44,900 --> 00:11:45,430
Think.

154
00:11:45,440 --> 00:11:45,950
Yeah.

155
00:11:46,490 --> 00:11:47,540
Close your eyes.

156
00:11:47,540 --> 00:11:53,900
That that is the next step is let's try to console.log the actual data that is past.

157
00:11:54,860 --> 00:12:06,290
Past data and now let's end or let's send something sets that red dot right and I'm going to say that

158
00:12:06,290 --> 00:12:17,630
post created and let's end a response here as red dot and now we are done with our implementation.

159
00:12:17,960 --> 00:12:26,240
So let's shut down a server again, clear the console and now clear the console and now let's restart

160
00:12:26,240 --> 00:12:26,630
it.

161
00:12:26,630 --> 00:12:27,950
And now check it out.

162
00:12:28,070 --> 00:12:28,700
Sorry.

163
00:12:28,700 --> 00:12:34,310
Inside here you see that the server has hangar because at first we're not returning anything to the

164
00:12:34,310 --> 00:12:34,820
user.

165
00:12:34,820 --> 00:12:41,360
So now let's try again sent and now we get a response called post created.

166
00:12:41,360 --> 00:12:52,700
And if you check the console, we see that we have received the data us that guys this is crazy.

167
00:12:52,700 --> 00:12:53,780
I really like it.

168
00:12:54,080 --> 00:13:00,710
Well, if we are using Express, I'm going to write a few lines of code to get this job done.

169
00:13:00,860 --> 00:13:02,120
The next video.

170
00:13:02,270 --> 00:13:03,650
Let's continue.

