WEBVTT

1
00:00:00.160 --> 00:00:02.540
Hello everyone and welcome back.

2
00:00:02.560 --> 00:00:04.260
In this video, we're going to be going

3
00:00:04.290 --> 00:00:09.580
over how to create a post request and why
we might need to use body parser

4
00:00:09.610 --> 00:00:12.860
middleware to extract the data
from the request body.

5
00:00:12.890 --> 00:00:17.740
But first, let's understand what body
parser is and why we need it exactly.

6
00:00:17.760 --> 00:00:21.380
In node JS and Express,
when a client sends a request

7
00:00:21.400 --> 00:00:26.020
to the server, the request is
received as a stream of data.

8
00:00:26.050 --> 00:00:30.780
The data can be in various formats
such as JSON, text, or binary.

9
00:00:30.810 --> 00:00:36.540
The body parser middleware is used
to extract the data from the request body

10
00:00:36.570 --> 00:00:40.380
and make it available in a convenient
format for us to use.

11
00:00:40.410 --> 00:00:42.580
The body parser middleware parses

12
00:00:42.610 --> 00:00:46.500
the incoming request body
and extracts the data.

13
00:00:46.530 --> 00:00:50.180
Without using body parser,
we would have to manually parse

14
00:00:50.210 --> 00:00:55.020
the request body ourselves, which can
be time consuming and prone to errors.

15
00:00:55.040 --> 00:00:57.020
There are different types of body parsers

16
00:00:57.050 --> 00:01:01.540
available for different data formats
such as JSON text or URL encoded data.

17
00:01:01.570 --> 00:01:07.660
And in our case, we'll be using JSON body
parser, which is used to parse JSON data.

18
00:01:07.690 --> 00:01:10.490
So what we're going to have to do for this

19
00:01:10.520 --> 00:01:14.260
is first we're going to have
to install the body parser.

20
00:01:14.290 --> 00:01:16.620
So for that, just go to your terminal,

21
00:01:16.650 --> 00:01:20.460
make sure that you're in the root
directory of your project where you have

22
00:01:20.490 --> 00:01:23.561
server JS defined,

23
00:01:23.761 --> 00:01:29.020
and run
npm install body-parser

24
00:01:29.050 --> 00:01:31.210
this will make body parser available

25
00:01:31.240 --> 00:01:35.780
for you, and you can just
import it from here.

26
00:01:35.810 --> 00:01:38.100
And if you want to use body parser

27
00:01:38.130 --> 00:01:47.930
with your app, all you have to do
is type app use body parser JSON.

28
00:01:47.960 --> 00:01:50.210
After this, body parser will be available

29
00:01:50.240 --> 00:01:54.980
to you and we can start testing
different request types.

30
00:01:55.010 --> 00:01:58.570
So let's go over the first request type.

31
00:01:58.600 --> 00:02:01.570
We have created a Get method before

32
00:02:01.600 --> 00:02:04.540
which is used to request
a resource from a server.

33
00:02:04.570 --> 00:02:09.700
It's a read only operation, meaning
it doesn't modify data on the server.

34
00:02:09.730 --> 00:02:13.420
I need to start my server
nodemon server JS.

35
00:02:13.450 --> 00:02:16.060
Now my server is started on port 3000.

36
00:02:16.090 --> 00:02:20.500
And then if I send this request,
I'm going to receive hello world back.

37
00:02:20.530 --> 00:02:26.580
So we could create a similar
Get request and name this user.

38
00:02:26.610 --> 00:02:34.730
And here we can write,
you are getting a user data back.

39
00:02:34.760 --> 00:02:38.170
And now if we send this request

40
00:02:38.200 --> 00:02:43.020
and make it user,
we're going to get this text back.

41
00:02:43.050 --> 00:02:48.860
And if we were to use something else
that does not exist, such as, let's say,

42
00:02:48.890 --> 00:02:54.380
nata my name,
you're going to get an error back because

43
00:02:54.410 --> 00:02:59.340
we can't get the route Nata
because we haven't set it up.

44
00:02:59.370 --> 00:03:00.380
Okay, great.

45
00:03:00.410 --> 00:03:03.740
So now that we understand how to use get

46
00:03:03.770 --> 00:03:08.220
requests, let's talk about
how to use post requests.

47
00:03:08.250 --> 00:03:11.740
Post requests are used to submit data.

48
00:03:11.770 --> 00:03:15.700
It's going to submit a data to a server
to create or update a resource.

49
00:03:15.730 --> 00:03:20.300
It's a write operation, meaning
that it can modify data on the server.

50
00:03:20.330 --> 00:03:24.580
So let's create a post request
to create a post request.

51
00:03:24.610 --> 00:03:31.740
All you do is app post
and you can use any route that you want.

52
00:03:31.770 --> 00:03:34.900
Let's create one on the user here as well.

53
00:03:34.930 --> 00:03:40.220
Let's get the request parameters
and respond with something.

54
00:03:40.250 --> 00:03:45.940
If we want to see
what we send in as parameters what we

55
00:03:45.970 --> 00:03:51.284
could do is just console log, request body]

56
00:03:51.484 --> 00:03:55.980
and to send in parameters using a post

57
00:03:56.010 --> 00:04:00.785
request we can use postman so we can make

58
00:04:00.985 --> 00:04:05.560
this a post request and set this to user.

59
00:04:06.440 --> 00:04:12.100
And then we can just go to the body
of this select row data.

60
00:04:12.130 --> 00:04:16.460
And here we could just select JSON
because that's the format that we chose.

61
00:04:16.480 --> 00:04:19.220
And here we can just write some JSON data

62
00:04:19.250 --> 00:04:25.980
such as Username for example,
and it's going to be Nata.

63
00:04:26.010 --> 00:04:27.380
And if this is JSON,

64
00:04:27.410 --> 00:04:34.300
actually we need to put this as a string
so that it's a viable JSON data.

65
00:04:34.330 --> 00:04:37.260
And now if I click on send

66
00:04:37.280 --> 00:04:42.460
here in my terminal,
you're going to see username Nata printed

67
00:04:42.480 --> 00:04:45.220
out because that's
the parameter that we sent.

68
00:04:45.250 --> 00:04:49.060
If you want to send more data,
you're most welcome to.

69
00:04:49.090 --> 00:04:53.380
You can say that first name for example,

70
00:04:53.410 --> 00:04:59.300
is Nata and the last name is Vacheishvili.

71
00:04:59.330 --> 00:05:03.540
And then if you send this again,
you're going to receive the updated data,

72
00:05:03.570 --> 00:05:06.660
which means that your server,
when you make a post request,

73
00:05:06.690 --> 00:05:11.580
would receive this data and it can use
this data for anything such as updating

74
00:05:11.600 --> 00:05:17.580
database fields and making sure that new
user Nata exists in the database.

75
00:05:17.600 --> 00:05:22.860
But we're not going to be going over how
to update the databases in this section.

76
00:05:22.890 --> 00:05:25.180
If you're more interested in serverside

77
00:05:25.210 --> 00:05:29.540
development, you can take another course
on it so that you get more familiar

78
00:05:29.570 --> 00:05:34.340
with more complex functionalities that
server side development can offer to you.

79
00:05:34.360 --> 00:05:41.140
Now you see here that this is still
sending the request and this is still

80
00:05:41.170 --> 00:05:45.140
sending the request because we
haven't responded with anything.

81
00:05:45.170 --> 00:05:52.300
So if we want to respond with something,
then we just can use response send here

82
00:05:52.330 --> 00:05:58.820
and we can say that we created a user with

83
00:05:58.850 --> 00:06:06.080
first name of request body first name.

84
00:06:06.240 --> 00:06:08.300
Now let's save this,

85
00:06:08.330 --> 00:06:16.140
let's go here and send this again
and we're getting some kind of an error.

86
00:06:16.170 --> 00:06:19.300
Let's see why we're getting this error.

87
00:06:19.330 --> 00:06:24.220
Oh, because I put a comma
here instead of plus.

88
00:06:24.250 --> 00:06:28.580
So let's save this again
and send this request.

89
00:06:28.600 --> 00:06:32.500
And here you're going to see that we
created a user with first name of Nata.

90
00:06:32.530 --> 00:06:35.700
If I change this and make it Nicolas

91
00:06:35.730 --> 00:06:39.380
for example, and send it,
you're going to see Nicolas appear here,

92
00:06:39.410 --> 00:06:44.140
which means that server is responding
with the data that you sent to it.

93
00:06:44.170 --> 00:06:48.220
So this is how easy it is
to make a post request.

94
00:06:48.250 --> 00:06:54.220
Post request, put request and delete
request are very similar to each other.

95
00:06:54.250 --> 00:07:01.180
So I'm just going to
duplicate this code and call this Delete

96
00:07:01.210 --> 00:07:05.180
and I'm going to duplicate this
and call this Put as well.

97
00:07:05.210 --> 00:07:07.180
And here we're going to say that we

98
00:07:07.210 --> 00:07:11.740
updated a user with the first
name of whatever we sent in.

99
00:07:11.760 --> 00:07:13.180
And here we're going to say that we

100
00:07:13.210 --> 00:07:18.700
deleted a user with first
name of the given values.

101
00:07:18.720 --> 00:07:19.100
Great.

102
00:07:19.120 --> 00:07:21.700
So we don't need this console
logs, or maybe we do.

103
00:07:21.720 --> 00:07:24.260
Why not?
You're going to see them appear here.

104
00:07:24.290 --> 00:07:27.900
And then here we can just change the type

105
00:07:27.920 --> 00:07:32.020
and say that we're sending
a delete request and send it.

106
00:07:32.040 --> 00:07:33.940
And here you're going to receive

107
00:07:33.960 --> 00:07:37.740
an appropriate message because server will
understand what type of request you're

108
00:07:37.770 --> 00:07:41.540
making and make sure that it
runs the appropriate function.

109
00:07:41.570 --> 00:07:45.940
So here it runs we deleted a user
with first name of Nicolas.

110
00:07:45.970 --> 00:07:49.580
And here we can also send a put
request because we set it up.

111
00:07:49.600 --> 00:07:52.820
And if I send this, it will
say that we updated the value.

112
00:07:52.850 --> 00:07:55.820
And here we also see the console logs

113
00:07:55.850 --> 00:07:59.580
from these two functions that we
just ran on our servers.

114
00:07:59.600 --> 00:08:03.100
Great.
So now we understand how to make a get

115
00:08:03.130 --> 00:08:07.140
request, how to make a post request,
how to make delete and put requests as

116
00:08:07.170 --> 00:08:12.220
well, and how to send and test
the data using postman.

117
00:08:12.250 --> 00:08:15.540
Now I also want to discuss one edge case.

118
00:08:15.570 --> 00:08:19.220
As you see here, we have used the same

119
00:08:19.250 --> 00:08:24.020
route with different request types
and it works perfectly fine.

120
00:08:24.040 --> 00:08:26.100
But if I were to use the same route

121
00:08:26.130 --> 00:08:32.820
with the same request type,
such as user get, for example,

122
00:08:32.850 --> 00:08:37.620
the first instance of this is going
to run and this is never going to run.

123
00:08:37.650 --> 00:08:39.220
So let's console log,

124
00:08:39.250 --> 00:08:47.020
this is never going to run here and we
can send something different back here.

125
00:08:47.050 --> 00:08:53.500
You cannot get this response back. Great,

126
00:08:53.530 --> 00:08:54.820
so let's save this.

127
00:08:54.850 --> 00:08:59.580
Our server is rerunning because it
detected the changes and now we can just

128
00:08:59.610 --> 00:09:04.260
send in the get request for the user
and you're going to get back.

129
00:09:04.290 --> 00:09:09.020
You are getting a user data back
and that is run here because it only

130
00:09:09.050 --> 00:09:13.060
recognizes the one instance
if you have duplicated routes.

131
00:09:13.080 --> 00:09:15.940
So make sure that you never create

132
00:09:15.970 --> 00:09:21.220
duplicated routes because otherwise some
part of your code might never run.

133
00:09:21.250 --> 00:09:23.100
So that's all for this video.

134
00:09:23.120 --> 00:09:24.460
Thanks so much for watching.

135
00:09:24.480 --> 00:09:25.600
I'll see you in the next one.

