1 00:00:04,500 --> 00:00:10,260 In this lesson we're going to talk about using location blocks in the engine configuration. 2 00:00:10,260 --> 00:00:15,630 This is the most used context in any engineered configuration and it's how will define and configure 3 00:00:15,660 --> 00:00:19,750 the behaviour of specific you are our eyes or requests. 4 00:00:19,770 --> 00:00:25,530 Think of location blocks as intercepting a request based on its value and then doing something other 5 00:00:25,530 --> 00:00:32,410 than just trying to serve a matching file relative to the root directory as we saw in the previous lesson. 6 00:00:32,550 --> 00:00:38,610 This standard behaviour is great for static resources such as our style sheet to this image etc.. 7 00:00:38,880 --> 00:00:46,290 But if we request for example slash greet or any other you are my engine x cannot match a file and instead 8 00:00:46,290 --> 00:00:49,050 serves this default foro for page. 9 00:00:49,170 --> 00:00:55,410 So let's see how to use a custom location context and intercept that greet request and give it some 10 00:00:55,410 --> 00:00:59,410 hypothetical purpose back to the engine X configuration file. 11 00:00:59,410 --> 00:01:03,300 I'll add a location context inside the server context. 12 00:01:03,540 --> 00:01:08,650 But this time the context takes a parameter before opening the context braces. 13 00:01:08,790 --> 00:01:11,700 This parameter being the you are right to match. 14 00:01:11,700 --> 00:01:18,660 So in our case slash greet meaning we can now respond worth redirect or do whatever else we need to 15 00:01:18,660 --> 00:01:22,920 do with this request and essentially manipulate the response. 16 00:01:22,920 --> 00:01:29,070 Let's just return a text string for now say return which takes two arguments. 17 00:01:29,130 --> 00:01:34,770 The response code and the response data to return to the client 200. 18 00:01:34,770 --> 00:01:40,780 So okay with a response body of hello from Engine X greet location. 19 00:01:42,590 --> 00:01:45,560 Save and reload the configuration. 20 00:01:47,850 --> 00:01:48,440 Refresh. 21 00:01:48,460 --> 00:01:49,660 Greet. 22 00:01:49,690 --> 00:01:50,640 And as expected. 23 00:01:50,640 --> 00:01:53,790 We get that string return to us now. 24 00:01:53,820 --> 00:01:58,920 There are several ways of matching you our eyes on location blocks and the one we just used is called 25 00:01:58,950 --> 00:02:00,390 the prefix match. 26 00:02:00,480 --> 00:02:02,010 Meaning that it matches a request. 27 00:02:02,010 --> 00:02:05,250 You are I starting with Slash greet. 28 00:02:05,370 --> 00:02:11,450 So in other words slash greeting is also matched or even another path. 29 00:02:11,580 --> 00:02:14,640 Literally anything starting with Slash greet. 30 00:02:15,590 --> 00:02:17,630 Should we however not want this behaviour. 31 00:02:17,630 --> 00:02:19,880 We can also do an exact match. 32 00:02:20,610 --> 00:02:24,660 By adding the equal sign or modifier before the pattern. 33 00:02:25,530 --> 00:02:28,410 Change that response to show us the update. 34 00:02:29,990 --> 00:02:32,030 Reload the configuration. 35 00:02:32,760 --> 00:02:34,360 Refresh. 36 00:02:34,500 --> 00:02:38,460 And now we get a 4 0 4 as only slash greet is matched. 37 00:02:38,490 --> 00:02:45,240 And this one no longer exists but if we simply navigate to slash greet we get that new message from 38 00:02:45,240 --> 00:02:47,160 the exact match location. 39 00:02:47,560 --> 00:02:53,280 Okay so prefix and exact matches are great but we can go one step further and matsue our eyes with a 40 00:02:53,280 --> 00:02:54,880 regular expression. 41 00:02:54,900 --> 00:03:00,570 This being the reason for installing the PCR library when we installed engine X this time we'll use 42 00:03:00,630 --> 00:03:06,990 a told the character as the match modifier to indicate that this is a regular expression match. 43 00:03:07,020 --> 00:03:12,810 I'll again do a match for Slash greet and let's say any number from 0 to 9. 44 00:03:12,840 --> 00:03:20,880 After that used this same return statement with a new message safe reload the configuration. 45 00:03:23,130 --> 00:03:27,290 Still at slash greet We now get a foro for as of course that matches. 46 00:03:27,300 --> 00:03:33,550 Also looking for a number from 0 to 9 after slash greet add a number to the you are I however. 47 00:03:33,570 --> 00:03:36,380 And again it works very importantly. 48 00:03:36,420 --> 00:03:40,110 Note that this told a modifier is case sensitive. 49 00:03:40,530 --> 00:03:45,100 So if we change one of these characters to an upper case like so. 50 00:03:45,970 --> 00:03:47,600 We're back to a foro for. 51 00:03:49,990 --> 00:03:56,350 To match case insensitive then so disregard that you are right case all together we can simply add an 52 00:03:56,440 --> 00:03:59,780 asterisk after that older like so. 53 00:04:01,050 --> 00:04:02,420 With a new message. 54 00:04:03,690 --> 00:04:11,360 Save reload dry slash greet with the uppercase character Ghen and now it works. 55 00:04:11,360 --> 00:04:13,830 Engine exercise is a priority to each of these. 56 00:04:13,830 --> 00:04:20,140 Location match modifiers with a regular expression being more important than a prefix match. 57 00:04:20,160 --> 00:04:23,730 We can test this by uncomment ing the original prefix. 58 00:04:23,730 --> 00:04:26,730 Make their capital G too. 59 00:04:26,790 --> 00:04:32,700 Exactly as we have it in the browser whilst also leaving the case insensitive regular expression match 60 00:04:33,360 --> 00:04:34,350 safe. 61 00:04:36,400 --> 00:04:43,090 Reload and the regular expression matched all ones regardless even if the order in which these location 62 00:04:43,090 --> 00:04:50,780 blocks are configured the final modifier is what's called preferential prefix which we write as carit 63 00:04:51,040 --> 00:04:52,440 told her. 64 00:04:52,540 --> 00:04:58,720 This is essentially the same as the basic prefix modifier only more important than a regular expression 65 00:04:58,720 --> 00:05:01,470 match save and reload. 66 00:05:03,530 --> 00:05:06,540 The prefix wins over the regular expression. 67 00:05:06,590 --> 00:05:11,420 It's important to understand the order of priority in which engine X matches requests. 68 00:05:11,420 --> 00:05:14,230 So to sum up they are as follows. 69 00:05:14,360 --> 00:05:16,600 Number one is the exact match. 70 00:05:16,760 --> 00:05:22,310 Should an exact match exist for a you are right that will always be the location block used regardless 71 00:05:22,310 --> 00:05:24,620 of any other matching location block. 72 00:05:24,620 --> 00:05:27,340 Second is the preferential prefix. 73 00:05:27,350 --> 00:05:33,520 Again this is the same as the basic prefix match but have priority over regular expression matches. 74 00:05:33,530 --> 00:05:36,510 Third in equal order is the regular expression. 75 00:05:36,510 --> 00:05:43,010 Matches should however both a case sensitive and case in sensitive location match your request. 76 00:05:43,040 --> 00:05:45,070 Not that this should ever really happen. 77 00:05:45,170 --> 00:05:51,220 The first block will be used as engineers has no other way of prioritizing one over the other. 78 00:05:51,470 --> 00:05:57,090 And finally the lowest of the lot is the very basic prefix matched that we looked at first. 79 00:05:57,230 --> 00:06:01,820 Any other match from the variations above will be prioritized over this one. 80 00:06:02,320 --> 00:06:07,990 That covers the basics of creating location context and matching you are rice in the next list and will 81 00:06:07,990 --> 00:06:10,700 cover the basics of Engineers variables.