1
00:00:04,590 --> 00:00:07,840
Let's get started with Express Server.

2
00:00:07,860 --> 00:00:12,750
That is how we can create server using express.

3
00:00:13,200 --> 00:00:20,490
Well, to better understand express server or how to create server with express, I advise you to watch

4
00:00:20,490 --> 00:00:23,680
the video and Node.js web server.

5
00:00:23,700 --> 00:00:30,180
In that way, you better understand why Express and why Express is so fantastic.

6
00:00:30,210 --> 00:00:33,800
So again, we have also explained what is a server.

7
00:00:33,810 --> 00:00:37,350
So let's go ahead and get started with Express Server.

8
00:00:37,740 --> 00:00:38,460
All right.

9
00:00:38,460 --> 00:00:46,650
So the format that we are going to use is what you are going to use any time you want to write or develop

10
00:00:46,680 --> 00:00:49,480
API or project using Node.js.

11
00:00:49,500 --> 00:00:54,360
So I'm going to take my time to explain every step that you're going to go through.

12
00:00:54,540 --> 00:00:54,960
All right.

13
00:00:54,960 --> 00:01:00,180
So the first step is, as usual, we need to create a folder for your project.

14
00:01:00,180 --> 00:01:05,980
So for this, because I am just practicing around with Express Server, I'm going to make use of I'm

15
00:01:06,030 --> 00:01:11,820
k d I r mean, I'm going to create a folder called Express Server.

16
00:01:13,100 --> 00:01:15,060
You can name it the name of your project.

17
00:01:15,080 --> 00:01:16,390
The choice is yours.

18
00:01:16,390 --> 00:01:22,790
So let me hit enter and let me see the end to the express server and let me open using my preferred

19
00:01:22,790 --> 00:01:23,720
code editor.

20
00:01:23,750 --> 00:01:26,390
That is Visual Studio code.

21
00:01:26,600 --> 00:01:28,760
So now let's get started with Express.

22
00:01:28,760 --> 00:01:32,510
The first step is we need to install experts.

23
00:01:32,510 --> 00:01:40,970
So let's go to the official website for Express that is Express JS dot com and you can see that it is

24
00:01:40,970 --> 00:01:46,220
fast and opinionated and minimalist web framework for Node.js.

25
00:01:46,220 --> 00:01:55,610
So it takes away all the abstractions away from us to make it pretty easy to write Node.js code, to

26
00:01:55,610 --> 00:01:57,210
develop APIs.

27
00:01:57,410 --> 00:02:01,430
And also Express is being hosted on NPM.

28
00:02:01,430 --> 00:02:04,310
So you can find Express on NPM.

29
00:02:04,310 --> 00:02:07,010
So if it's such for X press.

30
00:02:08,250 --> 00:02:10,080
Express NPM.

31
00:02:10,080 --> 00:02:17,610
Let's see and let's start for the first one, Express and PM and let's see the number of downloads and

32
00:02:17,610 --> 00:02:19,560
more details about experts.

33
00:02:19,570 --> 00:02:25,410
Liquid say that weekly download is around 24 million per week.

34
00:02:25,770 --> 00:02:27,750
How awesome this framework is.

35
00:02:27,750 --> 00:02:33,540
So before we can install Express, we need to do a couple of steps here.

36
00:02:33,960 --> 00:02:40,320
The first step is any time you start developing Node.js application, this is what you are going to

37
00:02:40,320 --> 00:02:40,860
do.

38
00:02:41,220 --> 00:02:49,860
The first step is we need to initialize our project as PM, meaning that we are going to use NPM packages

39
00:02:49,860 --> 00:02:51,600
in our projects, for example.

40
00:02:51,600 --> 00:02:54,930
Xpress Right, so let's go ahead and do that.

41
00:02:54,930 --> 00:02:59,940
So first step is NPM init, meaning NPM initialize.

42
00:02:59,940 --> 00:03:08,040
If we want to know more about NPM init, we can type NPM help and then init and let's see more details

43
00:03:08,040 --> 00:03:11,460
about that command it is used to create.

44
00:03:11,460 --> 00:03:17,580
What package JSON file do you remember on the web server of Node.js?

45
00:03:17,580 --> 00:03:26,520
We talk about this file and here again and description it can use to create or set up a new or existing

46
00:03:26,520 --> 00:03:27,990
NPM package.

47
00:03:28,140 --> 00:03:34,150
And the initialize in this case is an NPM package name, create and initialize it.

48
00:03:34,170 --> 00:03:38,130
So this is how we are going to use let me press control c to create.

49
00:03:38,130 --> 00:03:46,740
Now let me issue the command again and p m init so again npm initialize so hit enter.

50
00:03:46,920 --> 00:03:51,750
Now NPM is asking us a couple of questions about our project.

51
00:03:51,990 --> 00:03:58,860
The first question is what package name, meaning the name of the folder that we want to use because

52
00:03:58,860 --> 00:04:00,840
we have specified express folder.

53
00:04:00,840 --> 00:04:08,790
That's why by default it has selected or we can override it, let's say my API or my server, but I

54
00:04:08,790 --> 00:04:11,340
like it in this way, so I'm going to hit enter.

55
00:04:11,820 --> 00:04:15,180
The next step is the version of my project.

56
00:04:15,180 --> 00:04:15,900
So let's see.

57
00:04:15,900 --> 00:04:25,140
For the first time the rest of my project is going to be as 2.1.1 it's up to you hit enter and here

58
00:04:25,140 --> 00:04:27,510
give description about your projects.

59
00:04:27,510 --> 00:04:30,930
Let's say that creating.

60
00:04:32,040 --> 00:04:33,120
Express.

61
00:04:34,090 --> 00:04:34,740
Sarah.

62
00:04:35,200 --> 00:04:41,650
The next is entry point, which means that what going to be the main file name that we want to use,

63
00:04:41,800 --> 00:04:48,670
like I told you and that Node.js server creation that for backend development or for main files or back

64
00:04:48,670 --> 00:04:56,440
end development, we often use index or app or server, but I prefer to use server JS so I'm going to

65
00:04:56,440 --> 00:05:00,520
override this default configuration and hit enter.

66
00:05:00,910 --> 00:05:05,530
The next is test command, meaning a command for tests in your project.

67
00:05:05,740 --> 00:05:12,610
In our case, we are not going to do any test case, so I'll hit enter and then git repository tool

68
00:05:12,850 --> 00:05:15,400
for the meantime to let's ignore this one.

69
00:05:15,400 --> 00:05:21,210
And now the keyword for H2 purposes, let's say just this one for zero purposes.

70
00:05:21,850 --> 00:05:26,080
The author is going to be the name the developer.

71
00:05:26,080 --> 00:05:35,560
I know what academy and the last is hit enter the line space by default choosing IAC and let me hit

72
00:05:35,560 --> 00:05:38,980
enter and now ask me is this okay?

73
00:05:38,980 --> 00:05:42,070
Why not then our press okay or enter?

74
00:05:42,100 --> 00:05:51,550
Now we see I have object with properties if I check the file this what I have and now these are the

75
00:05:51,550 --> 00:06:00,250
configuration I use inside that and all this name can be overridden, meaning we can change the property

76
00:06:00,250 --> 00:06:00,880
names here.

77
00:06:00,940 --> 00:06:06,160
For example, the name of my folder, the version and description, and I see that.

78
00:06:06,160 --> 00:06:13,240
So and the key words here, you see we are specified here and the author has that now we are done with

79
00:06:13,240 --> 00:06:14,710
the configuration.

80
00:06:14,740 --> 00:06:20,950
The next step is we need to install express as we saw on the page.

81
00:06:21,130 --> 00:06:28,510
So for installation of packages, make sure that you install the exact version as I'm going to install

82
00:06:28,600 --> 00:06:29,500
in that way.

83
00:06:29,530 --> 00:06:35,800
That is, in future your code would be the same as my because I used to have this kind of problem with

84
00:06:35,800 --> 00:06:36,700
my students.

85
00:06:36,730 --> 00:06:42,400
Maybe at the time of recording this video I'm going to use Express version point four, maybe after

86
00:06:42,400 --> 00:06:45,190
three years, four years to be an update.

87
00:06:45,190 --> 00:06:48,040
In that way, the code base will change a little bit.

88
00:06:48,040 --> 00:06:52,150
In that case, your code and my code will never be the same.

89
00:06:52,150 --> 00:06:57,700
So to avoid this, make sure that you install the exact version as I'm going to install.

90
00:06:57,700 --> 00:07:04,870
And if you understand version one of any package for any update, you can play around with it and know

91
00:07:04,870 --> 00:07:06,010
how it works.

92
00:07:06,010 --> 00:07:12,940
So let's see the version of NPM at the time of recording this video, which is 4.1, 8.1.

93
00:07:12,940 --> 00:07:15,070
So let's go ahead and install that.

94
00:07:15,070 --> 00:07:16,150
So here we go.

95
00:07:16,150 --> 00:07:24,340
MP Am I going to be X Press at 4.1, 8.1.

96
00:07:24,340 --> 00:07:26,260
Let's confirm if it is true.

97
00:07:26,860 --> 00:07:27,820
Yes.

98
00:07:27,820 --> 00:07:29,950
So now let's hit Enter.

99
00:07:30,280 --> 00:07:35,830
Now Express is installing the packages for me, so let's have some patience now.

100
00:07:35,830 --> 00:07:37,870
Indeed, Express has finished.

101
00:07:37,870 --> 00:07:41,680
When I open my project I see a folder code.

102
00:07:41,680 --> 00:07:43,030
Not this one too.

103
00:07:43,030 --> 00:07:50,230
We have talked about it before, so this folder contains all the dependencies that our application need.

104
00:07:50,290 --> 00:07:55,360
For the meantime, we have installed experts and we have this bunch of folders.

105
00:07:55,360 --> 00:08:02,230
So these folders or this files are all needed by express to make our life easier.

106
00:08:02,230 --> 00:08:05,980
So we are not going to touch the Node.js module.

107
00:08:06,010 --> 00:08:08,260
Then this file is going to be packaged.

108
00:08:08,260 --> 00:08:15,670
That locked JSON file is because I am using two package managers, for example John and PM.

109
00:08:15,670 --> 00:08:17,500
That's why I have this.

110
00:08:17,500 --> 00:08:23,740
But in your case, if you are using only one John or PM, you are not going to see this one.

111
00:08:23,740 --> 00:08:27,280
So let me delete this one and that is it.

112
00:08:27,310 --> 00:08:29,770
Next is we need to create our file.

113
00:08:29,800 --> 00:08:37,809
Remember that inside the package we specify that the main file is supposed to be server, so we need

114
00:08:37,809 --> 00:08:41,440
to create the exact file going to be server.

115
00:08:42,400 --> 00:08:43,730
Sarah Dodd.

116
00:08:44,650 --> 00:08:48,820
And now the first step is we need to require experts.

117
00:08:48,820 --> 00:08:56,680
So here comes remember and import and export required then x press.

118
00:08:56,680 --> 00:09:01,530
So let's console.log x price and see what express has for us.

119
00:09:01,540 --> 00:09:03,490
So back to my terminal here.

120
00:09:03,490 --> 00:09:07,180
Let me click the console and run the application again.

121
00:09:08,020 --> 00:09:08,680
Right.

122
00:09:08,710 --> 00:09:12,770
We're going to be node going to be server dogs or server.

123
00:09:12,770 --> 00:09:16,330
And you can see again, we have a bunch of methods.

124
00:09:16,330 --> 00:09:23,770
The express is going to make our life easier and we have use for injecting middleware, route engine

125
00:09:23,770 --> 00:09:27,160
for rendering, template engine, the path and many more.

126
00:09:27,160 --> 00:09:30,400
So let's go ahead and create a server with Express.

127
00:09:30,400 --> 00:09:38,770
So we are going to create instance of Express as an app and then we assign to express as that meaning

128
00:09:38,770 --> 00:09:45,900
that express is a function and then we assign the result coming back and assigned a variable called

129
00:09:45,910 --> 00:09:52,720
app because functions are first class citizen, we can assign a variable to a function as this.

130
00:09:53,020 --> 00:09:55,600
The next step is let's create server.

131
00:09:55,900 --> 00:10:00,040
So let's see how we are going to create a server with express on the app.

132
00:10:00,040 --> 00:10:06,940
We have a method called listen as we discussed before and you pass in a port, let's say for this one,

133
00:10:06,940 --> 00:10:13,930
let's pass in 9000, then this one going to be a callback function and let me use normal function called

134
00:10:13,930 --> 00:10:15,370
here as that.

135
00:10:15,370 --> 00:10:27,760
And inside I'm going to say server is up and running and with few lines of code I'm able to create a

136
00:10:27,760 --> 00:10:28,750
server.

137
00:10:29,260 --> 00:10:34,570
All right, so let's run the application and let's see if our server is going to be connected.

138
00:10:34,570 --> 00:10:39,430
And indeed, we see I see server is up and running.

139
00:10:39,970 --> 00:10:44,320
Do you remember under Node.js how we able to create a server?

140
00:10:44,350 --> 00:10:45,970
Let me show you the code for that.

141
00:10:46,090 --> 00:10:53,140
So this is a code for creating a server in vanilla Node.js with some two lines of routing here.

142
00:10:53,200 --> 00:10:55,570
But with Express, there is no routing here.

143
00:10:55,570 --> 00:11:00,070
But instead I am able to run my server how easy it is.

144
00:11:00,370 --> 00:11:00,790
All right.

145
00:11:00,790 --> 00:11:07,990
So now that we're able to create our server now, we haven't done yet unless we respond to a request

146
00:11:07,990 --> 00:11:09,460
through routing.

147
00:11:09,460 --> 00:11:15,370
So in the next video, let's see how we can implement routing and express JS.

