1
00:00:02,200 --> 00:00:04,100
Now in the previous lectures,

2
00:00:04,100 --> 00:00:07,910
I talked about handling errors that can occur.

3
00:00:07,910 --> 00:00:10,730
And I talked about the 404 error,

4
00:00:10,730 --> 00:00:13,650
which means that a resource is not found.

5
00:00:13,650 --> 00:00:16,219
An error, typically cost by the client

6
00:00:16,219 --> 00:00:18,737
requesting an invalid resource

7
00:00:18,737 --> 00:00:21,870
and I talked about the 500 error,

8
00:00:21,870 --> 00:00:24,390
which is a service site error.

9
00:00:24,390 --> 00:00:29,020
Now these numbers are really just the status codes that we

10
00:00:29,020 --> 00:00:33,950
typically use for communicating these kind of errors.

11
00:00:33,950 --> 00:00:38,610
And I did talk about status codes before already.

12
00:00:38,610 --> 00:00:43,230
I did say that that we typically have this 200 status code

13
00:00:43,230 --> 00:00:48,060
as part of the response to signal that the request succeeded

14
00:00:48,060 --> 00:00:52,290
and we are able to send back a valid response

15
00:00:52,290 --> 00:00:57,290
and that'd be, for example, have 404 or 500 for the errors

16
00:00:57,490 --> 00:01:00,070
I mentioned in the last lectures.

17
00:01:00,070 --> 00:01:05,069
There also are other status codes, like 401, which is,

18
00:01:05,069 --> 00:01:05,903
for example,

19
00:01:05,903 --> 00:01:10,903
typically sent back if a user is not logged in and tries to

20
00:01:10,980 --> 00:01:14,940
access a resource that requires logging in.

21
00:01:14,940 --> 00:01:15,773
For example,

22
00:01:15,773 --> 00:01:19,470
if you try to purchase a product on Amazon and you want to

23
00:01:19,470 --> 00:01:22,700
go to the checkout page without being locked in,

24
00:01:22,700 --> 00:01:24,920
that could be scenarios for that.

25
00:01:24,920 --> 00:01:29,350
We'll see that status code 401 later.

26
00:01:29,350 --> 00:01:32,640
There also are many other status codes.

27
00:01:32,640 --> 00:01:36,370
Attached you find a link to all the available status codes

28
00:01:36,370 --> 00:01:38,780
and we'll see some of them throughout this course,

29
00:01:38,780 --> 00:01:39,954
the most common ones.

30
00:01:39,954 --> 00:01:44,430
But there is one problem with those status codes.

31
00:01:44,430 --> 00:01:46,800
It's nice that we have them

32
00:01:46,800 --> 00:01:51,800
and it's nice that I named my files 404 EJS and 500 EJS.

33
00:01:53,570 --> 00:01:56,320
With that, at least to us as a developer,

34
00:01:56,320 --> 00:02:00,120
it's clear which template is used in which scenarios.

35
00:02:00,120 --> 00:02:02,600
But if we have a look at the running application,

36
00:02:02,600 --> 00:02:06,170
we actually don't see the status codes there.

37
00:02:06,170 --> 00:02:09,870
If I enter an invalid, URL here.

38
00:02:09,870 --> 00:02:13,840
If I try to access a restaurant that doesn't exist,

39
00:02:13,840 --> 00:02:16,080
I get this 404 page

40
00:02:16,080 --> 00:02:19,668
and that is all a typical user should see.

41
00:02:19,668 --> 00:02:24,668
But technically we are not returning a totally clear or

42
00:02:26,330 --> 00:02:28,320
correct response

43
00:02:28,320 --> 00:02:30,560
and you can see that this is the case

44
00:02:30,560 --> 00:02:33,620
if you open your developer tools and go to the network

45
00:02:33,620 --> 00:02:34,573
tab there.

46
00:02:35,680 --> 00:02:37,800
If I reload this page,

47
00:02:37,800 --> 00:02:40,180
we see all the requests that were made

48
00:02:40,180 --> 00:02:42,500
and if we click on the top most one,

49
00:02:42,500 --> 00:02:46,560
which is the request to the HTML page itself,

50
00:02:46,560 --> 00:02:49,683
I indeed get this kind of status code,

51
00:02:49,683 --> 00:02:52,710
a 304, not modified.

52
00:02:52,710 --> 00:02:57,710
Now here, we actually see a 304 status code instead of 200.

53
00:02:58,200 --> 00:03:00,780
The only reason for that is that Chrome,

54
00:03:00,780 --> 00:03:05,650
the browser I'm using here is actually caching old responses

55
00:03:05,650 --> 00:03:06,483
for me,

56
00:03:06,483 --> 00:03:11,320
which means it's basically saved these downloaded HTML files

57
00:03:11,320 --> 00:03:16,180
or the HTML content locally on my computer to serve them

58
00:03:16,180 --> 00:03:19,400
quicker for a subsequent requests,

59
00:03:19,400 --> 00:03:22,330
and that's why I'm seeing a different status code here.

60
00:03:22,330 --> 00:03:26,220
I could disable this by checking this disabled cash check

61
00:03:26,220 --> 00:03:28,110
mark here in the network tab

62
00:03:28,110 --> 00:03:32,270
and then I would see the 200 response.

63
00:03:32,270 --> 00:03:34,570
Since I haven't disabled this here,

64
00:03:34,570 --> 00:03:38,233
If I reload this, I get the 304 response.

65
00:03:39,330 --> 00:03:42,390
But 300 dish status codes,

66
00:03:42,390 --> 00:03:45,560
so status codes in the 300 hundred area,

67
00:03:45,560 --> 00:03:48,950
as well as 200 dish status codes

68
00:03:48,950 --> 00:03:52,240
mean that the request was successful

69
00:03:52,240 --> 00:03:56,420
and you did get the response that you requested in the end.

70
00:03:56,420 --> 00:03:58,570
And that's the wrong message here,

71
00:03:58,570 --> 00:04:01,880
because this is not the response we requested.

72
00:04:01,880 --> 00:04:06,820
We want the details for the restaurant, with this ID.

73
00:04:06,820 --> 00:04:11,000
And what we got is a page that tells us that this restaurant

74
00:04:11,000 --> 00:04:16,000
doesn't exist. That's not a success, that's a failure.

75
00:04:16,160 --> 00:04:19,720
And therefore we should also communicate that with the

76
00:04:19,720 --> 00:04:23,970
status code, by setting a errors status code here,

77
00:04:23,970 --> 00:04:28,620
even though that won't affect, what's visible on the page.

78
00:04:28,620 --> 00:04:31,380
That's important, that won't change.

79
00:04:31,380 --> 00:04:34,320
The page content will always be what you see here,

80
00:04:34,320 --> 00:04:38,040
but still we want to communicate the correct status code,

81
00:04:38,040 --> 00:04:40,540
the technically correct status code

82
00:04:40,540 --> 00:04:43,820
to the browser so that the browser, for example,

83
00:04:43,820 --> 00:04:48,560
does not save cache this error page locally,

84
00:04:48,560 --> 00:04:51,930
but that the browser knows that for this request,

85
00:04:51,930 --> 00:04:55,060
so for a get request to this URL,

86
00:04:55,060 --> 00:04:58,240
we did not get a success response.

87
00:04:58,240 --> 00:05:01,513
We want to make that clear and at the moment we aren't.

88
00:05:02,430 --> 00:05:04,470
And to be technically correct,

89
00:05:04,470 --> 00:05:07,940
you can dare for all the call and never method on this

90
00:05:07,940 --> 00:05:12,450
response object here before you call render.

91
00:05:12,450 --> 00:05:14,903
And that's the status method.

92
00:05:16,290 --> 00:05:19,410
Now what I'm doing here is called chaining.

93
00:05:19,410 --> 00:05:23,030
I'm calling a method after calling another method.

94
00:05:23,030 --> 00:05:26,730
So I'm calling this render method on the result of calling

95
00:05:26,730 --> 00:05:30,330
status and status simply returns us

96
00:05:30,330 --> 00:05:32,673
an updated response object.

97
00:05:33,540 --> 00:05:36,180
So I'm technically still calling render on my

98
00:05:36,180 --> 00:05:40,700
response object, but on a slightly updated one.

99
00:05:40,700 --> 00:05:43,300
And what did change well,

100
00:05:43,300 --> 00:05:47,970
status allows me to set a custom status code

101
00:05:47,970 --> 00:05:51,070
and the default would be 200.

102
00:05:51,070 --> 00:05:54,150
Here, I do want to set it to 404

103
00:05:54,150 --> 00:05:57,240
and hence me simply pass 404 as a

104
00:05:57,240 --> 00:05:58,723
number to status.

105
00:06:00,370 --> 00:06:05,370
And here, when we render 500, I will set this to status 500.

106
00:06:08,090 --> 00:06:12,220
And in the other place where we have a 404 error,

107
00:06:12,220 --> 00:06:13,320
Here,

108
00:06:13,320 --> 00:06:16,040
when we don't find a restaurant with that ID,

109
00:06:16,040 --> 00:06:20,793
I also call status first and set this to 404.

110
00:06:23,180 --> 00:06:25,670
Now, if we save all of that

111
00:06:25,670 --> 00:06:27,570
and we opened the developer tools again,

112
00:06:27,570 --> 00:06:29,720
if I reload this page now,

113
00:06:29,720 --> 00:06:31,840
you'll see the request is a red,

114
00:06:31,840 --> 00:06:35,323
and now we have data appropriate status code here.

115
00:06:37,130 --> 00:06:40,950
Now to the user who doesn't use these developer tools,

116
00:06:40,950 --> 00:06:43,220
nothing changes on the screen.

117
00:06:43,220 --> 00:06:45,660
Still, this is technically more correct

118
00:06:45,660 --> 00:06:48,600
and it can have an impact, for example,

119
00:06:48,600 --> 00:06:52,470
on some optimizations, the browser might perform.

120
00:06:52,470 --> 00:06:55,090
That's why we should be correct technically

121
00:06:55,090 --> 00:06:58,060
even if the user doesn't directly see

122
00:06:58,060 --> 00:06:59,883
that changed status code.

123
00:07:01,230 --> 00:07:02,670
Now over the next lectures,

124
00:07:02,670 --> 00:07:06,720
you won't see that code anymore because I added this after

125
00:07:06,720 --> 00:07:08,770
recording the next lectures,

126
00:07:08,770 --> 00:07:11,320
because originally I planned on showing this later,

127
00:07:11,320 --> 00:07:12,700
but plans changed

128
00:07:12,700 --> 00:07:16,180
and this is important to understand right now,

129
00:07:16,180 --> 00:07:21,180
because this now is a technically valid 404 response with

130
00:07:21,845 --> 00:07:26,845
our 404 page content and the appropriate status code to

131
00:07:27,400 --> 00:07:29,950
clearly communicate to the client,

132
00:07:29,950 --> 00:07:32,660
to the browser, that this is a response,

133
00:07:32,660 --> 00:07:34,193
but not the expected one.

