1
00:00:03,650 --> 00:00:06,464
In the previous lessons,

2
00:00:06,464 --> 00:00:08,580
we have learned about Node.

3
00:00:08,580 --> 00:00:13,215
We have also seen how Node can be used to set up a Web server.

4
00:00:13,215 --> 00:00:20,460
Now the Node designers intentionally kept node small with a small number of

5
00:00:20,460 --> 00:00:24,480
code modules so that they can leave it up to

6
00:00:24,480 --> 00:00:29,730
third party developers to come up with innovative solutions to problems.

7
00:00:29,730 --> 00:00:37,590
So once Node was released a number of third party developers started designing

8
00:00:37,590 --> 00:00:45,875
and releasing interesting third-party modules that can run on top of the Node platform.

9
00:00:45,875 --> 00:00:50,030
Now you have seen a number of Node modules that could be

10
00:00:50,030 --> 00:00:54,185
used for front-end development in the previous courses.

11
00:00:54,185 --> 00:00:59,240
Now in this course we are looking at server-side development and on the server-side,

12
00:00:59,240 --> 00:01:03,020
one of the most popular third party Node modules or

13
00:01:03,020 --> 00:01:08,720
frameworks for building HTTP servers is Express.

14
00:01:08,720 --> 00:01:11,480
Let's look at some details of Express in

15
00:01:11,480 --> 00:01:14,345
this lecture and then we will make use of Express to

16
00:01:14,345 --> 00:01:21,725
build a server that serves up the REST API throughout the rest of this course.

17
00:01:21,725 --> 00:01:25,670
First, what is Express?

18
00:01:25,670 --> 00:01:30,355
Express is a fast, unopinionated,

19
00:01:30,355 --> 00:01:37,519
minimalist framework that runs on top of Node.js and supports Web development.

20
00:01:37,519 --> 00:01:42,570
This is the definition that I borrowed from expressjs.com,

21
00:01:42,570 --> 00:01:48,025
the site where Express related documentation is available for us.

22
00:01:48,025 --> 00:01:53,769
Now Express allows you to develop a Web application,

23
00:01:53,769 --> 00:02:02,385
server-side application that will serve up content for consumption by our front-end.

24
00:02:02,385 --> 00:02:07,730
Express provides a robust set of features which we will explore in

25
00:02:07,730 --> 00:02:13,610
more detail through the rest of the lessons in this course.

26
00:02:13,610 --> 00:02:17,425
Express itself as I said is a minimalist framework,

27
00:02:17,425 --> 00:02:21,020
and Express also provides a way of extending and adding

28
00:02:21,020 --> 00:02:25,465
functionality to Express through third-party middleware.

29
00:02:25,465 --> 00:02:31,830
This extends the functionality of Express and adds in more features as required.

30
00:02:31,830 --> 00:02:38,750
So, you can build your Web server using just as many third-party middleware as

31
00:02:38,750 --> 00:02:46,070
required for meeting the needs of your Web server that you're designing.

32
00:02:46,070 --> 00:02:52,130
We'll look at some examples of third-party middleware in this lesson and we

33
00:02:52,130 --> 00:02:58,170
will also explore more of these as we go through the rest of the lessons in this course.

34
00:02:58,170 --> 00:03:01,520
To use Express in your project, of course,

35
00:03:01,520 --> 00:03:05,730
the first step is to install Express and since Express is a Node module,

36
00:03:05,730 --> 00:03:10,940
we install it by saying npm install express --save

37
00:03:10,940 --> 00:03:17,145
and this would install Express into your local project.

38
00:03:17,145 --> 00:03:21,865
We will see the use of this in the exercise that follows this lecture.

39
00:03:21,865 --> 00:03:25,670
Let's briefly talk about the Express middleware.

40
00:03:25,670 --> 00:03:28,425
So what exactly is the purpose of middleware?

41
00:03:28,425 --> 00:03:32,510
The middleware that Express supports provide a lot of

42
00:03:32,510 --> 00:03:38,370
plug-in functionality that would be used to enhance your Express application,

43
00:03:38,370 --> 00:03:41,165
plug-in functionality like for example we will look at

44
00:03:41,165 --> 00:03:45,500
a middleware called Morgan which allows you to

45
00:03:45,500 --> 00:03:52,490
print out log information to the screen about the requests that come into your server.

46
00:03:52,490 --> 00:03:56,494
Similarly, we'll look at another middleware called BodyParser,

47
00:03:56,494 --> 00:03:59,090
which allows you to parse the body of

48
00:03:59,090 --> 00:04:02,375
the incoming HTTP request message and extract

49
00:04:02,375 --> 00:04:06,450
information from it for use within your Express application.

50
00:04:06,450 --> 00:04:11,395
We'll see the use of these in the exercise that follows.

51
00:04:11,395 --> 00:04:18,455
As I mentioned, Morgan does logging of information to the console on the server-side,

52
00:04:18,455 --> 00:04:21,645
information about the incoming requests.

53
00:04:21,645 --> 00:04:28,145
Similarly, they can serve up static Web resources from our server using the Express

54
00:04:28,145 --> 00:04:35,855
static so this will serve up information from a folder within our Express project,

55
00:04:35,855 --> 00:04:39,785
and in declaring the project you can say I'm

56
00:04:39,785 --> 00:04:44,690
double underscore filename and double underscore directory name or dirname which gives

57
00:04:44,690 --> 00:04:48,410
you the full path for the file or the directory for

58
00:04:48,410 --> 00:04:53,535
the current module and you will see me using that in the exercise.

59
00:04:53,535 --> 00:04:55,640
Now that we have understood a little bit about

60
00:04:55,640 --> 00:04:58,695
Express and the middleware that Express uses,

61
00:04:58,695 --> 00:05:02,930
let's look at a Node module

62
00:05:02,930 --> 00:05:06,499
because this is the first time we're encountering a third party Node modules,

63
00:05:06,499 --> 00:05:10,460
we'll look at some details about the third party Node modules so if we'll examine the

64
00:05:10,460 --> 00:05:15,000
package.json file to see what is contained in the package.json file,

65
00:05:15,000 --> 00:05:17,920
we'll also look at semantic versioning.

66
00:05:17,920 --> 00:05:22,550
So when you specify the version of the package that you use,

67
00:05:22,550 --> 00:05:31,745
you always specify the version by specifying the Major Version.Minor Version.the patch.

68
00:05:31,745 --> 00:05:35,685
So when you install a package,

69
00:05:35,685 --> 00:05:39,230
it is always identified by these three numbers,

70
00:05:39,230 --> 00:05:45,440
major version which might introduce breaking changes so which means that if you are

71
00:05:45,440 --> 00:05:48,560
installing a newer version of package it may not be

72
00:05:48,560 --> 00:05:51,920
completely backward compatible with previous versions.

73
00:05:51,920 --> 00:05:55,370
It may introduce breaking changes whereby you may need to go back and fix

74
00:05:55,370 --> 00:05:59,905
the code that you might have written in the earlier version of your project.

75
00:05:59,905 --> 00:06:01,790
The minor version introduces

76
00:06:01,790 --> 00:06:06,385
some minor changes to your package and may not be breaking changes.

77
00:06:06,385 --> 00:06:14,800
A patch would be a bug fix that is often issued then a small bug is discovered.

78
00:06:14,800 --> 00:06:21,005
So patches usually do not lead to any breaking changes and so you can easily use

79
00:06:21,005 --> 00:06:24,395
a higher version or a higher patch version of

80
00:06:24,395 --> 00:06:30,215
a particular package that you're using within your Node application.

81
00:06:30,215 --> 00:06:33,410
When you're installing a package you can specify

82
00:06:33,410 --> 00:06:38,465
the exact version of the package to install by saying npm install,

83
00:06:38,465 --> 00:06:40,460
for example if you want to install the

84
00:06:40,460 --> 00:06:44,630
4.0.0 version of Express you can say express@4.0.0.

85
00:06:44,630 --> 00:06:51,635
So you are explicitly specifying which version of the package to install.

86
00:06:51,635 --> 00:06:55,630
If you're okay with a higher level patch version,

87
00:06:55,630 --> 00:07:02,590
you would say npm install express@"~4.0.0 If

88
00:07:02,590 --> 00:07:07,190
a minor version higher version of a package is

89
00:07:07,190 --> 00:07:13,125
acceptable then you would say @ and the name of the package.

90
00:07:13,125 --> 00:07:17,210
Now this kind of information is also saved in the package.json file,

91
00:07:17,210 --> 00:07:20,070
we'll quickly pay a visit to the package of json file

92
00:07:20,070 --> 00:07:23,750
where you will notice some of this information being saved.

93
00:07:23,750 --> 00:07:27,575
Now when you do the exercise that follows this lecture

94
00:07:27,575 --> 00:07:31,845
you will see this in the package.json file.

95
00:07:31,845 --> 00:07:38,325
This will be the result of completing the exercise that follows this lecture.

96
00:07:38,325 --> 00:07:43,230
In the exercise we will construct a simple Web server using Express.

97
00:07:43,230 --> 00:07:46,570
Now let me take you through a quick tour of package.json

98
00:07:46,570 --> 00:07:50,470
to illustrate some information in package.json.

99
00:07:50,470 --> 00:07:56,530
So in package.json file you'll notice this information here,

100
00:07:56,530 --> 00:08:00,525
this property for our json that is stored here called Dependencies.

101
00:08:00,525 --> 00:08:04,250
The dependencies is where you will specify

102
00:08:04,250 --> 00:08:07,400
which additional third party modules on

103
00:08:07,400 --> 00:08:10,700
which this particular Node project is dependent upon,

104
00:08:10,700 --> 00:08:15,230
so as you can see here we are saying that this project is dependent upon Express

105
00:08:15,230 --> 00:08:20,540
and Morgan and note in particular how this information is specified here.

106
00:08:20,540 --> 00:08:25,715
So this is ^4.16.3 meaning that this will

107
00:08:25,715 --> 00:08:31,380
work with any version that is 4.16.3 or higher.

108
00:08:31,380 --> 00:08:35,725
You can use a higher-level minor version and this project will still be

109
00:08:35,725 --> 00:08:40,970
okay with it and similarly for Morgan we have specified the information here.

110
00:08:40,970 --> 00:08:47,345
So this additional information is added into the package.json file whenever you do

111
00:08:47,345 --> 00:08:55,035
npm install and say --save flag for the npm install.

112
00:08:55,035 --> 00:09:02,040
Now, also you will notice that I have created a.gitignore file here and inside

113
00:09:02,040 --> 00:09:09,770
the.gitignore file I have specified that git should ignore the Node modules folder.

114
00:09:09,770 --> 00:09:13,275
So what exactly is contained in the Node modules folder?

115
00:09:13,275 --> 00:09:20,704
If you install third party modules into your Node application,

116
00:09:20,704 --> 00:09:26,270
all these third party modules will be saved in the Node modules folder here,

117
00:09:26,270 --> 00:09:31,420
so within your project you will see that the Node modules folder has been created here.

118
00:09:31,420 --> 00:09:34,550
And taking a look at the node modules folder you will see

119
00:09:34,550 --> 00:09:38,495
a whole bunch of packages that have been installed.

120
00:09:38,495 --> 00:09:40,490
Now all of these have been installed because you

121
00:09:40,490 --> 00:09:43,250
install Express and Express in turn depends upon

122
00:09:43,250 --> 00:09:50,550
some other packages that are required so all those also get installed here by default.

123
00:09:50,550 --> 00:09:55,850
In particular, let me also draw your attention to the Express package here,

124
00:09:55,850 --> 00:09:59,045
so if you go into the Express package you will see

125
00:09:59,045 --> 00:10:02,470
additional information being stored in the Express package,

126
00:10:02,470 --> 00:10:06,435
so index perspective also since Express itself is

127
00:10:06,435 --> 00:10:10,924
a Node module you will see a package.json

128
00:10:10,924 --> 00:10:14,015
file inside Express also which contains

129
00:10:14,015 --> 00:10:19,865
additional information which of course is very detailed there,

130
00:10:19,865 --> 00:10:25,280
difficult for us to understand but note in particular

131
00:10:25,280 --> 00:10:31,330
that Express itself is dependent upon many other Node modules here.

132
00:10:31,330 --> 00:10:34,820
And that's the reason why all these other Node modules

133
00:10:34,820 --> 00:10:38,250
also have been installed into the Node modules folder.

134
00:10:38,250 --> 00:10:40,160
So when you install Express,

135
00:10:40,160 --> 00:10:45,020
this will immediately also trigger all its dependencies also to be installed because

136
00:10:45,020 --> 00:10:50,980
Express will require these other Node modules for it to do its work.

137
00:10:50,980 --> 00:10:54,590
Also, within Express also you'll see the index.js file.

138
00:10:54,590 --> 00:11:00,125
So this is the starting point for our Express Node module itself and

139
00:11:00,125 --> 00:11:05,690
note in particular that the index.js file simply says module.exports require lib express.

140
00:11:05,690 --> 00:11:12,645
So the actual code for the Express module itself is inside this lib project,

141
00:11:12,645 --> 00:11:16,225
lib folder here and you can see the detail.

142
00:11:16,225 --> 00:11:20,900
So if you are curious to see the details of Express itself you can go and look

143
00:11:20,900 --> 00:11:25,560
in there but again this may be a bit too much for you at this moment.

144
00:11:25,560 --> 00:11:31,815
For the moment just accept the fact that Express will do its work as expected.

145
00:11:31,815 --> 00:11:38,450
But I thought that it will be a interesting experience to visit the Node module's folder

146
00:11:38,450 --> 00:11:45,435
to look at a structure of one particular third-party Node module in a bit more detail.

147
00:11:45,435 --> 00:11:48,530
Also, you will notice that there is another file

148
00:11:48,530 --> 00:11:52,010
here called package-lock.json that is installed.

149
00:11:52,010 --> 00:11:56,690
Now this is being installed by the newer versions of npm.

150
00:11:56,690 --> 00:12:01,190
The package-lock.json file is automatically generated by

151
00:12:01,190 --> 00:12:06,259
npm which stores information about

152
00:12:06,259 --> 00:12:10,265
the exact tree that was generated when you install

153
00:12:10,265 --> 00:12:14,630
other Node modules and this is very useful when you

154
00:12:14,630 --> 00:12:22,660
need to do installation of the Node modules at another location.

155
00:12:22,660 --> 00:12:27,620
So for example if you download a Git repository and

156
00:12:27,620 --> 00:12:32,865
try to recreate this project on another computer,

157
00:12:32,865 --> 00:12:35,930
you would simply type npm install on the prompt and

158
00:12:35,930 --> 00:12:39,050
that'll prompt your Node application to

159
00:12:39,050 --> 00:12:45,810
automatically install everything that is specified in the dependencies here for you.

160
00:12:45,810 --> 00:12:53,335
While creating that the package-lock.json stores additional information that is used by

161
00:12:53,335 --> 00:13:01,805
npm to do the correct installation of all the npm modules that are required.

162
00:13:01,805 --> 00:13:05,090
Now for the moment you don't need to worry about the details of

163
00:13:05,090 --> 00:13:08,290
what is there inside the package-lock.json file.

164
00:13:08,290 --> 00:13:15,550
With this, we complete this lesson where we have examined some details about Express.