1
00:00:06,380 --> 00:00:09,290
Welcome to our second court challenge.

2
00:00:09,290 --> 00:00:11,570
So let's see what we are going to do.

3
00:00:12,020 --> 00:00:20,450
It says that create a middleware function that will check the number of requests made to the server.

4
00:00:21,230 --> 00:00:29,720
And if the number of requests is greater than ten return response, we start this code 4 to 9, which

5
00:00:29,720 --> 00:00:31,610
means too many requests.

6
00:00:32,369 --> 00:00:38,610
And if the number of requests is less than ten, we are going to call the next million word.

7
00:00:38,940 --> 00:00:46,320
So what we are going to do is that we are going to check how many requests has been made to our server.

8
00:00:46,530 --> 00:00:52,050
So by default we are going to allow only ten requests to the server.

9
00:00:52,380 --> 00:00:58,470
If a user make more than ten requests, we are going to through an error, say, is that too many requests?

10
00:00:58,830 --> 00:00:59,700
All right.

11
00:00:59,730 --> 00:01:00,810
Give it a shot.

12
00:01:06,860 --> 00:01:07,910
Welcome back.

13
00:01:07,940 --> 00:01:10,130
Now let me show you my solution.

14
00:01:10,130 --> 00:01:11,300
So here we go.

15
00:01:11,330 --> 00:01:14,390
Solution four, challenge number two.

16
00:01:14,660 --> 00:01:22,160
So the first step is I need to declare a variable that's going to keep track the number of requests.

17
00:01:22,430 --> 00:01:27,440
So I make use of LED because I won't override the value for this variable.

18
00:01:27,650 --> 00:01:34,920
So the variable name going to be as request count and by default I'll assign as zero.

19
00:01:34,940 --> 00:01:37,790
So now here comes our middle word.

20
00:01:38,060 --> 00:01:39,560
So cost.

21
00:01:40,920 --> 00:01:42,750
Request count.

22
00:01:45,230 --> 00:01:48,170
Is equal to arrow function.

23
00:01:48,170 --> 00:01:57,290
So let me change this one to request count value I start and for the function I have access to my request

24
00:01:57,290 --> 00:02:00,020
response and then the next.

25
00:02:00,470 --> 00:02:10,440
So inside my function body, I'm going to check if the request value count is more than ten.

26
00:02:10,460 --> 00:02:17,000
So inside the if statement, you are going to throw an error with status code for two nine, which means

27
00:02:17,000 --> 00:02:18,320
too many requests.

28
00:02:18,500 --> 00:02:24,040
So here we are going to return from this function and now on rest.

29
00:02:24,050 --> 00:02:26,090
Then we add a status code.

30
00:02:26,270 --> 00:02:33,930
Status code, and then I start 4 to 9 and next is the message.

31
00:02:33,950 --> 00:02:39,680
Let's use JSON data here and you provide too many.

32
00:02:41,070 --> 00:02:43,430
Request a house.

33
00:02:43,440 --> 00:02:47,270
Go ahead and then call next as that.

34
00:02:47,550 --> 00:02:47,880
Now.

35
00:02:47,910 --> 00:02:49,020
Moment of truth.

36
00:02:49,290 --> 00:02:56,610
So let me pass this one to app dot use because I want to provide this one to all of the route.

37
00:02:56,610 --> 00:02:59,820
So I'm going to be request counts.

38
00:03:00,300 --> 00:03:03,150
So inside the request.

39
00:03:03,630 --> 00:03:09,570
So inside the request counts, let's try to console.log the value in the console and see how many counts

40
00:03:09,570 --> 00:03:11,000
we have made to the server.

41
00:03:11,010 --> 00:03:14,220
So request count value.

42
00:03:14,460 --> 00:03:16,500
So now I'm back to Postman.

43
00:03:18,660 --> 00:03:20,040
Well, I see.

44
00:03:20,040 --> 00:03:20,670
Zero.

45
00:03:20,700 --> 00:03:22,020
Why is it so?

46
00:03:22,200 --> 00:03:26,270
The problem is that we are not updating the counts.

47
00:03:26,280 --> 00:03:27,180
Sorry, guys.

48
00:03:27,180 --> 00:03:34,140
So inside the body here for each request, you are going to update the value that is plus plus with

49
00:03:34,140 --> 00:03:34,920
plus one.

50
00:03:35,190 --> 00:03:38,100
So now with this one, our server has restarted.

51
00:03:38,130 --> 00:03:39,210
Let me make the request.

52
00:03:39,210 --> 00:03:41,730
The first one, I get one here.

53
00:03:41,760 --> 00:03:48,990
Second I get two and three, four or five until I hit more than 20 or ten.

54
00:03:49,080 --> 00:03:50,320
And let's see.

55
00:03:50,340 --> 00:03:51,540
Let me check.

56
00:03:51,720 --> 00:03:52,790
I see ten here.

57
00:03:52,800 --> 00:03:56,160
The last one going to be you see, I get a message.

58
00:03:56,160 --> 00:03:57,660
Too many requests.

