1
00:00:03,930 --> 00:00:06,395
In the previous lecture,

2
00:00:06,395 --> 00:00:14,130
we learned about the essentials of networking including details of the HTTP protocol.

3
00:00:14,130 --> 00:00:20,805
Now, we'll look at how Node can be used to construct an HTTP server.

4
00:00:20,805 --> 00:00:28,420
This is where the core HTTP module that is built into Node comes to our aid.

5
00:00:28,420 --> 00:00:33,820
In addition, we will look at a couple of other core Node modules that are useful for

6
00:00:33,820 --> 00:00:40,390
building an HTTP server from using Node.

7
00:00:40,390 --> 00:00:42,730
So, how do we make use of the Node HTTP module?

8
00:00:42,730 --> 00:00:50,045
As I mentioned, the HTTP module is one of the core modules that are built into the Node.

9
00:00:50,045 --> 00:00:53,275
So, this is a core networking module that supports

10
00:00:53,275 --> 00:00:58,475
a high-performance foundation for HTTP stack.

11
00:00:58,475 --> 00:01:02,440
So using the module within our Node application requires

12
00:01:02,440 --> 00:01:06,430
us to import the module into our application.

13
00:01:06,430 --> 00:01:12,190
So this is what we do by saying const HTTP = require and,

14
00:01:12,190 --> 00:01:15,120
the name of the module HTTP there.

15
00:01:15,120 --> 00:01:17,260
Now, once the module is imported,

16
00:01:17,260 --> 00:01:24,330
then this HTTP module supports a createServer function which takes,

17
00:01:24,330 --> 00:01:29,500
as a parameter, a function that acts as

18
00:01:29,500 --> 00:01:32,890
a callback function for the createServer module and

19
00:01:32,890 --> 00:01:37,240
this function has two parameters : request and response.

20
00:01:37,240 --> 00:01:40,120
The request is the request message that comes in from

21
00:01:40,120 --> 00:01:44,380
the client side and we can parse the request message and then extract

22
00:01:44,380 --> 00:01:48,010
a lot of information from the request message and use it to make

23
00:01:48,010 --> 00:01:51,850
decisions on how we construct the response message corresponding to that.

24
00:01:51,850 --> 00:01:56,665
The response message is constructed using the second parameter here, the res,

25
00:01:56,665 --> 00:02:01,855
on which we can construct the various header values for

26
00:02:01,855 --> 00:02:08,830
our HTTP response message and also the body of the HTTP response message.

27
00:02:08,830 --> 00:02:10,120
Now, to start the server,

28
00:02:10,120 --> 00:02:11,725
once you create the server,

29
00:02:11,725 --> 00:02:16,030
then you would say server.listen and then supply the port number

30
00:02:16,030 --> 00:02:20,420
and the host name for the server and that will start up our server.

31
00:02:20,420 --> 00:02:27,520
We'll look at some details in the examples exercise that follows this lecture.

32
00:02:27,520 --> 00:02:32,770
As I mentioned, the incoming request message information is available

33
00:02:32,770 --> 00:02:38,130
through the req parameter that the function takes in the createServer method.

34
00:02:38,130 --> 00:02:40,480
So, the req or request,

35
00:02:40,480 --> 00:02:42,460
if you want to give it full name,

36
00:02:42,460 --> 00:02:46,120
you can call it as a request but in general we have found that in

37
00:02:46,120 --> 00:02:50,425
examples we used req to represent the request message.

38
00:02:50,425 --> 00:02:57,462
It supports on the Javascript object properties like the header,

39
00:02:57,462 --> 00:03:02,225
the body and also various information that can be extracted,

40
00:03:02,225 --> 00:03:09,350
the URL and the method that has been requested by that client site.

41
00:03:09,350 --> 00:03:13,240
And, in response, you construct the response using

42
00:03:13,240 --> 00:03:18,424
the response Javascript object which supports that setHeader function,

43
00:03:18,424 --> 00:03:23,570
the statusCode function which can be set to the status code of the response message.

44
00:03:23,570 --> 00:03:26,235
And then you would write the message saying

45
00:03:26,235 --> 00:03:29,440
res.write and you would end the message by saying

46
00:03:29,440 --> 00:03:35,785
res.end and that takes the final part of the body for the message.

47
00:03:35,785 --> 00:03:40,225
The body may be either standard text or HTML or

48
00:03:40,225 --> 00:03:47,369
any other information that you want to enclose inside the body of that reply message.

49
00:03:47,369 --> 00:03:49,780
But once the res.end is called,

50
00:03:49,780 --> 00:03:54,460
the reply is sent back to the client from the HTTP server.

51
00:03:54,460 --> 00:03:59,185
Two other core modules that are useful for us

52
00:03:59,185 --> 00:04:04,295
when we're constructing the Node HTTP server is the Node path module.

53
00:04:04,295 --> 00:04:09,145
The path module enables us to specify the path to a file and then

54
00:04:09,145 --> 00:04:14,375
examine whether the file exists or examine more details about a file.

55
00:04:14,375 --> 00:04:17,620
For example, the extension of the file,

56
00:04:17,620 --> 00:04:19,185
name and so on.

57
00:04:19,185 --> 00:04:22,000
So, the path module can be used within

58
00:04:22,000 --> 00:04:26,360
our application by requiring the path module as shown here.

59
00:04:26,360 --> 00:04:28,360
And then, this supports methods,

60
00:04:28,360 --> 00:04:29,990
like for example, path.resolve,

61
00:04:29,990 --> 00:04:33,940
which will convert a relative path into

62
00:04:33,940 --> 00:04:38,620
an absolute path that the entire path to the file.

63
00:04:38,620 --> 00:04:43,930
You can also check the extension name for the file by calling the

64
00:04:43,930 --> 00:04:47,830
path.extname function and supply the file path that will

65
00:04:47,830 --> 00:04:52,640
examine the extension of the filename.

66
00:04:52,640 --> 00:04:55,870
Similarly, the file system module,

67
00:04:55,870 --> 00:04:57,970
again a core module in Node,

68
00:04:57,970 --> 00:05:00,670
will enable us to read and write

69
00:05:00,670 --> 00:05:04,805
files that exist in the local file system on the computer.

70
00:05:04,805 --> 00:05:08,725
So the file system modules can be used within

71
00:05:08,725 --> 00:05:13,480
our Node application by requiring it as shown here.

72
00:05:13,480 --> 00:05:16,765
And then, these file system modules supports many methods

73
00:05:16,765 --> 00:05:21,460
including a method for checking whether the file exists in the local file system or not.

74
00:05:21,460 --> 00:05:24,190
So the fs.exists will take

75
00:05:24,190 --> 00:05:28,165
the filePath as the first parameter and the second parameter is

76
00:05:28,165 --> 00:05:35,530
a callback function which will come back with a parameter which we can name it as exists.

77
00:05:35,530 --> 00:05:38,410
This parameter, exists, will be true if the file

78
00:05:38,410 --> 00:05:43,595
exists and will be false if the file does not exist within the local file system.

79
00:05:43,595 --> 00:05:50,390
Similarly, you can read from the file by creating a read stream given the file path.

80
00:05:50,390 --> 00:05:53,585
So you can say fs.createReadStream and

81
00:05:53,585 --> 00:05:58,240
the filePath and this can be piped in to the response message.

82
00:05:58,240 --> 00:06:01,175
So the file will be read in from the file

83
00:06:01,175 --> 00:06:05,290
given by the filePath and then the contents will be put

84
00:06:05,290 --> 00:06:13,220
into the body of the response message by calling a chained function as shown here.

85
00:06:13,220 --> 00:06:17,920
So, these are some examples of some core Node modules that will be very

86
00:06:17,920 --> 00:06:23,710
useful when we are constructing a HTTP server using Node.

87
00:06:23,710 --> 00:06:29,223
Now, that we understand some details about the HTTP server and how it can be constructed,

88
00:06:29,223 --> 00:06:30,690
let's go to the exercise,

89
00:06:30,690 --> 00:06:36,880
where we will construct a simple Node HTTP server which first will serve up

90
00:06:36,880 --> 00:06:41,950
some basic information and then after that we'll serve up files

91
00:06:41,950 --> 00:06:48,170
that exist in a public folder in our project.