1
00:00:03,810 --> 00:00:05,895
In the previous lecture,

2
00:00:05,895 --> 00:00:07,880
we learned about REST API.

3
00:00:07,880 --> 00:00:10,781
You have seen how the REST API endpoints

4
00:00:10,781 --> 00:00:14,990
support a way for a client application to be able to either retrieve

5
00:00:14,990 --> 00:00:23,100
data from the server or upload data to the server using the various HTTP operations.

6
00:00:23,100 --> 00:00:27,062
In this lecture and exercise that follows this lecture,

7
00:00:27,062 --> 00:00:30,985
we'll look specifically about what kind of support Express

8
00:00:30,985 --> 00:00:36,545
supports for designing and implementing a REST API-based server.

9
00:00:36,545 --> 00:00:40,085
In particular, we'll also look at the Express router,

10
00:00:40,085 --> 00:00:44,600
and how it enables us to subdivide our application and

11
00:00:44,600 --> 00:00:50,285
then organize it into multiple mini Express-like applications,

12
00:00:50,285 --> 00:00:54,495
which combine together to form the Express application,

13
00:00:54,495 --> 00:01:00,605
specifically when we are dealing with various REST API and parts.

14
00:01:00,605 --> 00:01:02,985
So to summarize, in the previous lecture,

15
00:01:02,985 --> 00:01:06,315
we examined REST in detail.

16
00:01:06,315 --> 00:01:10,560
We also looked at how each endpoint is identified by a URI,

17
00:01:10,560 --> 00:01:13,950
and how we can specify the various operations to be done on

18
00:01:13,950 --> 00:01:18,295
each endpoint using the appropriate HTTP verb,

19
00:01:18,295 --> 00:01:20,880
the GET, PUT, POST, or DELETE.

20
00:01:20,880 --> 00:01:22,560
Now in this lecture,

21
00:01:22,560 --> 00:01:29,530
we'll look at how Express supports the development of a REST API server.

22
00:01:29,530 --> 00:01:35,070
We'll look at Express's support for various matters like app.all,

23
00:01:35,070 --> 00:01:38,335
app.get, put, post, and delete,

24
00:01:38,335 --> 00:01:43,305
and how these methods can be used to construct a REST API server.

25
00:01:43,305 --> 00:01:50,104
Within Express, the various application groups can be defined using the, app.

26
00:01:50,104 --> 00:01:51,955
and the various methods.

27
00:01:51,955 --> 00:01:59,180
So, the app.all specifies an operation that needs to be done on all the various verbs,

28
00:01:59,180 --> 00:02:00,990
on, in, and part.

29
00:02:00,990 --> 00:02:04,050
So for example, in this example,

30
00:02:04,050 --> 00:02:07,970
we see that the endpoint is defined by /dishes,

31
00:02:07,970 --> 00:02:13,230
and so the app.all, whatever is specified in the function that is given for app.all,

32
00:02:13,230 --> 00:02:16,830
will be applied to all the incoming requests.

33
00:02:16,830 --> 00:02:22,155
The app.get specifies what needs to be done for GET requests and,

34
00:02:22,155 --> 00:02:24,233
correspondingly, for the POST,

35
00:02:24,233 --> 00:02:30,015
PUT, and DELETE requests which are sent to the /dishes endpoint,

36
00:02:30,015 --> 00:02:32,700
as shown in this example.

37
00:02:32,700 --> 00:02:39,471
Express also supports defining the endpoints with parameters.

38
00:02:39,471 --> 00:02:45,750
So for example, you can specify a specific dish ID if you wish to,

39
00:02:45,750 --> 00:02:49,500
and then let the operation be performed for

40
00:02:49,500 --> 00:02:55,320
that specific endpoint referring to that specific dish with a given dish ID.

41
00:02:55,320 --> 00:02:58,890
So in this case, the dish ID itself is

42
00:02:58,890 --> 00:03:03,706
specified as a parameter and the pattern used for that is,

43
00:03:03,706 --> 00:03:05,460
as you see in this example,

44
00:03:05,460 --> 00:03:11,920
/dishes/: and then you would specify the parameter here.

45
00:03:11,920 --> 00:03:14,310
To make it easy for us to understand,

46
00:03:14,310 --> 00:03:18,060
I named the parameter as dishId in there.

47
00:03:18,060 --> 00:03:22,035
You can use any parameter name that you choose to, but again,

48
00:03:22,035 --> 00:03:27,290
using a meaningful name for a parameter makes it more easier to understand the code.

49
00:03:27,290 --> 00:03:28,815
So in this example,

50
00:03:28,815 --> 00:03:34,058
the :dishId means that if we issue a request to an endpoint,

51
00:03:34,058 --> 00:03:38,400
say for example, /dishes/23,

52
00:03:38,400 --> 00:03:45,300
then the dishId parameter enables us to extract this number 23 so that we

53
00:03:45,300 --> 00:03:48,420
can operate on dish number 23 within

54
00:03:48,420 --> 00:03:52,496
the function that is specified inside this method here.

55
00:03:52,496 --> 00:03:55,190
So in there, the parameter,

56
00:03:55,190 --> 00:04:01,440
the dishId parameter itself can be obtained by using the request object on which

57
00:04:01,440 --> 00:04:04,020
the params property you supported and

58
00:04:04,020 --> 00:04:08,885
the params property supports all the incoming request parameters,

59
00:04:08,885 --> 00:04:10,470
and dishId, in particular,

60
00:04:10,470 --> 00:04:13,710
is one of those request parameters which can be accessed,

61
00:04:13,710 --> 00:04:16,271
as shown in the code here.

62
00:04:16,271 --> 00:04:22,585
When you send a PUT or a POST request from the client to the server,

63
00:04:22,585 --> 00:04:28,545
you're often enclosing the data in the body of the message that is sent to the server.

64
00:04:28,545 --> 00:04:31,110
Now, which means that we need a method

65
00:04:31,110 --> 00:04:34,230
of extracting information from the body of the message.

66
00:04:34,230 --> 00:04:39,320
So this is where the body parser middleware for Express is very useful.

67
00:04:39,320 --> 00:04:44,805
So the body parser enables us to parse the information from the body of the message.

68
00:04:44,805 --> 00:04:47,515
To use the body parser, as we would expect,

69
00:04:47,515 --> 00:04:52,155
we would install the body-parser node module,

70
00:04:52,155 --> 00:04:56,070
and then require it within our Express application,

71
00:04:56,070 --> 00:04:59,550
and then specify app.use(bodyParser).

72
00:04:59,550 --> 00:05:03,570
And if the body contains data in the JSON format,

73
00:05:03,570 --> 00:05:05,810
you can say bodyParser.json,

74
00:05:05,810 --> 00:05:09,561
so which means that this will parse only data that is in

75
00:05:09,561 --> 00:05:14,250
JSON format that is enclosed in the body of that request message.

76
00:05:14,250 --> 00:05:16,145
In particular, in the exercise,

77
00:05:16,145 --> 00:05:22,620
we will be parsing incoming data which is sent in the form of a JSON string.

78
00:05:22,620 --> 00:05:24,930
The body parser, as you would expect,

79
00:05:24,930 --> 00:05:30,480
parses the body of the message and populates the req.body property.

80
00:05:30,480 --> 00:05:32,190
So, on the request,

81
00:05:32,190 --> 00:05:36,250
the body property will contain whatever is parsed in from

82
00:05:36,250 --> 00:05:41,600
the body of the request message by body parser.

83
00:05:41,600 --> 00:05:45,030
If you are implementing an Express application

84
00:05:45,030 --> 00:05:48,465
which supports multiple REST API endpoints,

85
00:05:48,465 --> 00:05:53,265
then it makes sense to subdivide the code into

86
00:05:53,265 --> 00:05:59,520
multiple modules and then make use of them to construct the overall Express application.

87
00:05:59,520 --> 00:06:03,347
So this where the Express router comes to our aid.

88
00:06:03,347 --> 00:06:08,070
An Express router defines many Express application,

89
00:06:08,070 --> 00:06:10,045
and within that many Express application,

90
00:06:10,045 --> 00:06:11,480
you can, for example,

91
00:06:11,480 --> 00:06:16,550
deal with one particular REST API endpoint in more detail,

92
00:06:16,550 --> 00:06:20,215
or one particular pattern of REST API endpoint in more detail.

93
00:06:20,215 --> 00:06:27,656
So, for example, we can define a dishRouter as express.Router,

94
00:06:27,656 --> 00:06:32,450
and then the dishRouter can then handle the endpoints.

95
00:06:32,450 --> 00:06:35,420
So when you express something as express.Router,

96
00:06:35,420 --> 00:06:38,570
it supports the route endpoint.

97
00:06:38,570 --> 00:06:41,015
One of the reasons for using Express router,

98
00:06:41,015 --> 00:06:42,455
as you would realize,

99
00:06:42,455 --> 00:06:45,470
is that if we use the standard app GET,

100
00:06:45,470 --> 00:06:47,142
PUT, POST, and DELETE methods,

101
00:06:47,142 --> 00:06:48,760
for each one of these methods,

102
00:06:48,760 --> 00:06:52,315
you need to explicitly specify the REST API endpoints.

103
00:06:52,315 --> 00:06:55,130
One advantage of using Express router is that if you

104
00:06:55,130 --> 00:06:59,422
say the router.Route and then specify the endpoint,

105
00:06:59,422 --> 00:07:07,220
that endpoint will be applied to all the methods and all the various GET, PUT, POST,

106
00:07:07,220 --> 00:07:11,060
and DELETE verb-related methods can all be chained

107
00:07:11,060 --> 00:07:16,575
together into the route in defining the code for our application.

108
00:07:16,575 --> 00:07:22,075
We'll look at more details of this in the exercise that follows this lecture.

109
00:07:22,075 --> 00:07:27,505
With this quick understanding of how Express supports REST API endpoint,

110
00:07:27,505 --> 00:07:31,070
let's move on to the exercise where we will construct

111
00:07:31,070 --> 00:07:35,682
their support for the /dishes REST API endpoint.

112
00:07:35,682 --> 00:07:38,180
As part of the first assignment,

113
00:07:38,180 --> 00:07:40,160
you will be further extending

114
00:07:40,160 --> 00:07:44,559
this Express application to support additional REST API endpoints,

115
00:07:44,559 --> 00:07:48,990
including /promotions and /leaders.

116
00:07:48,990 --> 00:07:52,040
If you have taken the previous courses in the specialization,

117
00:07:52,040 --> 00:07:55,220
you will immediately begin to see why we are supporting

118
00:07:55,220 --> 00:08:00,810
all these different REST API endpoints on other server side.