WEBVTT 00:06.200 --> 00:13.220 In this video you're going to get introduced to the CCP built in module in those jars. 00:13.220 --> 00:23.600 So the HP module is used to transfer data using the CCP protocol which stands for the Hypertext Transfer 00:23.810 --> 00:24.890 Protocol. 00:24.890 --> 00:33.570 So this is the typical task of a web server which you can now create using Node James. 00:33.890 --> 00:43.370 So first of all we will need to require this module using the require method. 00:47.830 --> 00:50.690 It's called a CCP. 00:51.320 --> 01:00.450 Alright so what we want to do in this session is to create a server using this a CCP module. 01:01.270 --> 01:10.150 So to create a server you will need to create to use a method of ATP called Create server. 01:10.150 --> 01:11.410 So this method 01:15.590 --> 01:21.170 will take as a parameter a callback function. 01:21.470 --> 01:30.080 So this is the callback function which will be executed when a user makes a request to our server. 01:30.080 --> 01:31.430 So let's call it respond 01:34.640 --> 01:41.220 and also let's store the output of the method inside a variable. 01:41.220 --> 01:44.430 I'm going to call server. 01:44.720 --> 01:46.940 So this is our server. 01:46.970 --> 01:54.140 So now what we need to do is create a function response which will be executed when a user makes a request 01:54.140 --> 01:54.830 to our server. 01:55.520 --> 02:01.240 So this is for creating a server. 02:01.380 --> 02:04.190 And now we need to create the callback 02:25.680 --> 02:29.680 so our function is going to be called respond. 02:29.720 --> 02:38.670 So this function will take a couple of promises a request and a response so you can call these what 02:38.700 --> 02:39.970 you can call them anything you like. 02:39.990 --> 02:44.100 You can call them rec and rest but it's for clarity. 02:44.100 --> 02:46.830 I'm going to call them request and response 02:59.080 --> 03:05.860 the request variable is something that's the server is going to receive when the user makes a request 03:05.920 --> 03:07.410 to the server. 03:07.630 --> 03:14.260 So it includes various bits of information like the U.R.L. submitted by the user and so on. 03:14.260 --> 03:20.470 Now the response is another object which comes with so many methods which you can use to build a response 03:21.280 --> 03:23.590 which you will send back to the user. 03:24.190 --> 03:30.490 So for now let's just write something really simple and just console dot log 03:34.000 --> 03:37.180 the requests variable. 03:37.840 --> 03:45.750 Okay so if you're on this code like this then it's not going to work because you are missing an extra 03:45.750 --> 03:53.140 step which is really important which is to listen to the user requests. 03:53.880 --> 04:04.580 So to do this you need to use a method of your server which you just created called listen. 04:04.660 --> 04:15.960 So this method will take a few promises so it's going to take a first bomb into the port so the port 04:16.020 --> 04:20.040 is like an end point of communication. 04:20.540 --> 04:20.990 So 04:23.820 --> 04:32.760 there aren't many ports which should be available on your system and you need to choose a port that 04:32.760 --> 04:35.910 is available on the system in question so that it works. 04:37.260 --> 04:48.470 So for example we can choose a port three thousand in local hosts so let's write our connection settings 04:48.470 --> 04:48.710 here 04:52.940 --> 04:56.880 so let's start with the port. 04:57.170 --> 05:02.700 I'm going to go for 3000 you can choose another port if you like. 05:02.700 --> 05:03.180 So 05:06.990 --> 05:07.380 again. 05:07.410 --> 05:16.960 So a port is an end point of communication. 05:17.060 --> 05:17.950 All right. 05:17.980 --> 05:21.010 And also you will lead a hostname. 05:21.290 --> 05:24.100 Now the hostname is an optional is. 05:24.190 --> 05:25.950 Inside the listen method. 05:25.970 --> 05:28.280 Is going to use it for now. 05:28.390 --> 05:29.680 So the hostname 05:32.110 --> 05:39.190 is the IP which is associated with your device. 05:39.200 --> 05:39.540 Okay. 05:39.580 --> 05:41.400 Or with the device in question. 05:42.160 --> 05:48.400 So any device connected to a computer network has got its own IP. 05:48.400 --> 05:51.760 So IP which is 05:54.510 --> 06:09.220 associated with any device connected to a computer network to a computer network is like the worldwide 06:09.490 --> 06:10.630 web. 06:10.640 --> 06:10.940 All right. 06:10.960 --> 06:18.230 So let's define our host named variable so I'm gonna be using local hosts. 06:18.230 --> 06:19.770 In this example. 06:19.820 --> 06:27.160 So in this case it should be 1 2 7 points zero point zero point one. 06:27.500 --> 06:29.200 This is for local. 06:29.210 --> 06:39.990 HOST Now if you run this example on your own server then you need to use the correct IP of that server 06:40.620 --> 06:45.180 and use a port which is available on that server. 06:45.260 --> 06:50.610 OK so now let's move to the next parameter. 06:50.850 --> 06:54.110 This time is going to be a callback function. 06:54.300 --> 07:02.020 So this is the function which will be executed when you start listening to the server. 07:02.040 --> 07:09.420 So let's define this right here and let's just log something to the console like 07:13.620 --> 07:19.870 listening on port all by the post. 07:19.870 --> 07:20.200 No 07:24.930 --> 07:37.570 hostname followed by the host name that we chose. 07:37.930 --> 07:40.030 OK so I'm missing something right there. 07:40.890 --> 07:42.410 So let's save now. 07:42.450 --> 07:43.470 Let's run our app 07:46.590 --> 07:50.910 so you can see the outputs of our callback function. 07:51.030 --> 07:53.800 So listening on board and his. 07:54.620 --> 07:54.890 All right. 07:54.900 --> 08:03.360 So at the moment we are listening to user requests so let's send a request and see what we get. 08:03.360 --> 08:10.810 So to send a request we gonna go to Chrome then go to local hosts Google on three thousand. 08:11.280 --> 08:19.640 So at the moment the page is going to keep loading because we are not sending any response but we are 08:19.640 --> 08:23.270 interested in the value of the requests very well for now. 08:23.470 --> 08:30.050 And you can see it's a huge variable which contains so many bits of information. 08:31.100 --> 08:41.530 So what we can do we can log some specific properties of the request variable. 08:41.530 --> 08:44.240 So for example let's log the URL 08:46.950 --> 08:48.710 so we need to restart the server. 08:48.720 --> 08:56.520 So let's stop is using controlled C and then let's run the command again and let's send a request this 08:56.520 --> 09:01.590 time OK it's already sent. 09:01.600 --> 09:12.310 So it is sending again and you can see that our your URL is right there and that's because we haven't 09:12.310 --> 09:15.610 specified anything after the Port number. 09:15.610 --> 09:26.980 So if you try for example cars then that's the URL you get so the URL will refer to anything that you 09:26.980 --> 09:31.750 write following the port number. 09:31.750 --> 09:32.700 Okay. 09:32.950 --> 09:41.270 So now the page will keep loading and we don't get any response. 09:41.730 --> 09:49.100 So let's go back to our file and create a response which we will send back to the user. 09:50.340 --> 09:57.630 So first of all we're going to start with Response Headers so Response Headers they contain various 09:57.630 --> 10:05.590 bits of information about the file sends by the server like the content type etc.. 10:05.680 --> 10:06.900 All right. 10:07.440 --> 10:19.570 Now there are methods which you can use to set headers which are attached to the response object. 10:20.220 --> 10:25.100 So the first one we're gonna be using is set header. 10:25.300 --> 10:34.480 So this influx of set header is says Heather had a name and 10:39.080 --> 10:39.620 value. 10:41.420 --> 10:43.160 So let's try for example 10:47.230 --> 10:53.800 content type. 10:54.190 --> 11:01.460 Let's head back to just text plain text. 11:01.510 --> 11:05.070 So this is the syntax for it. 11:05.080 --> 11:05.340 All right. 11:05.350 --> 11:13.510 Now let's add another header and this time we're gonna use a another method called right head. 11:13.510 --> 11:17.200 So this method will take the stage is code 11:20.930 --> 11:24.770 and then the head is inside an object. 11:25.100 --> 11:33.410 So the city code will tell us how the requests went. 11:33.420 --> 11:40.680 So if you get a status code 200 This means that we get a successful response for 0 4 for example is 11:40.680 --> 11:46.230 usually used to say that the page was not found or something like that. 11:46.590 --> 11:46.980 So 11:50.470 --> 11:51.190 let's try this 11:55.210 --> 12:09.740 so it isn't like a city's code 200 for a successful response and let's add a header. 12:09.820 --> 12:16.460 So I'm going to go again for a content type and this time I'm going to set it 12:22.230 --> 12:27.290 to a schedule like this. 12:27.330 --> 12:34.670 Now what happens if you said the same header using this method and that's method. 12:34.740 --> 12:43.530 Well right head is going to be a given presidents in this case but if the head is are different types 12:43.860 --> 12:46.580 then they are just gonna be matched. 12:46.590 --> 12:47.490 All right. 12:47.580 --> 12:54.760 So in our case the content type is going to be set to this because you're going to give precedence to 12:54.880 --> 12:56.500 write it. 12:56.590 --> 12:56.830 All right. 12:56.860 --> 13:05.860 So now after this we can start building the body of our response. 13:06.460 --> 13:10.560 So to do this we're going to use response. 13:10.560 --> 13:11.330 That's right. 13:11.980 --> 13:18.340 So this one sends the body of the response. 13:18.440 --> 13:22.440 So this will allow us to show things on the web page. 13:22.550 --> 13:25.380 So we're gonna be writing some HMO. 13:25.430 --> 13:36.070 So what you can do you can go to a Web site like Jay has been to get a template like that's then let's 13:36.070 --> 13:37.000 use response. 13:37.030 --> 13:39.690 That's right. 13:41.270 --> 13:42.270 With our template 13:45.350 --> 13:46.060 okay. 13:46.160 --> 13:53.750 So let's change content inside its server side so I'm gonna go for node and inside the body. 13:53.750 --> 13:55.340 I'm going to add a paragraph 13:58.870 --> 14:00.700 node is fun 14:04.630 --> 14:13.180 so instead of using the right method only ones like that I'm going to split this so that I can customize 14:13.180 --> 14:14.240 it later on. 14:14.380 --> 14:18.850 So I'm going to put the paragraph in a separate statement 14:37.620 --> 14:45.010 like that and I'm going to push the end 14:49.450 --> 14:54.980 inside response dots and instead of response those right. 14:55.090 --> 14:55.840 A response. 14:55.880 --> 15:00.390 Those end marks the end of the server response. 15:00.390 --> 15:08.450 So that should be the last statements that you use when building the response of the server 15:37.250 --> 15:43.770 so let's try this and see where we get going to restart the server. 15:47.760 --> 15:52.050 We're missing a parenthesis right the OK. 15:52.090 --> 15:53.940 So let's try again. 16:00.450 --> 16:01.390 And here we go. 16:01.530 --> 16:08.600 We get our paragraph there and if we check the source we can see some valid a symbol. 16:08.610 --> 16:08.960 KATE 16:14.800 --> 16:23.540 Now you may have noticed that when we sense the first request we had to you URLs being printed there. 16:23.810 --> 16:26.090 So the first one which corresponds to that one. 16:26.230 --> 16:30.900 And there was another one here which is looking for something else. 16:31.180 --> 16:34.540 And this is your Web site icon. 16:34.660 --> 16:44.300 Basically any time you open a Web site if you don't specify the icon or the Web site's icon location 16:44.360 --> 16:51.650 then by default it's going to look for the icon in this path. 16:51.660 --> 16:52.640 This happens as well. 16:52.710 --> 17:05.870 When you check the source of the Web site it's going to look for the Web site icon again all right. 17:06.020 --> 17:21.780 So if you want to avoid that you can go back to the code and only print the requests your URL if that's 17:21.840 --> 17:24.980 you URL is different. 17:30.750 --> 17:31.260 From 17:35.080 --> 17:37.900 far we can not I. 17:38.730 --> 17:39.930 So now if you try 17:43.190 --> 17:50.860 again then you will avoid getting to requests like this. 17:51.020 --> 17:51.500 Perfect. 17:53.000 --> 17:58.330 Now I suggest testing your server requests and responses using postman. 17:58.460 --> 18:04.640 So if you haven't got spacemen you can always know ladies from guess Bozeman dot com. 18:04.910 --> 18:14.390 So what you can do you can just paste the URL right there and use the guest method and send the request 18:14.390 --> 18:15.330 like that. 18:15.350 --> 18:18.420 So this is the response. 18:18.800 --> 18:20.330 So does the 18:23.430 --> 18:26.340 body of the response. 18:26.430 --> 18:28.660 You can preview it if you like. 18:28.800 --> 18:30.090 These are the habits. 18:30.180 --> 18:32.650 So we said the content type ourselves. 18:32.910 --> 18:35.970 There are other headers which will be set for us. 18:36.390 --> 18:41.020 So this application is gonna be very useful when doing testing later on. 18:41.460 --> 18:47.850 So in the next video you're gonna go through a quick challenge using the HP module.