0:00
[MUSIC]
In the previous lessons, we have learned about Node.
We have also seen how Node can be used to set up a web server.
Now, the Node designers intentionally kept Node
small with a small number of core modules so
that they can leave it up to third-party developers to
come up with innovative solutions to problems.
So once Node was released, a number of third-party
developers started designing and releasing interesting
third-party modules that can run on top of the Node platform.
Now, you have seen a number of Node modules that could be used for
frontend development in the previous courses.
Now in this course, we are looking at server-side development.
And on the server side, one of the most popular third-party Node modules or
frameworks for building HTTP servers is Express.
Let's look at some details of Express in this lecture.
And then we will make use of Express to build a server that
serves up REST API throughout the rest of this course.
First, what is Express?
Express is a fast, unopinionated,
minimalist framework that runs on top of Node.js and
supports development.
This is the definition that I borrowed from expressjs.com,
the site where Express-related documentation is available for us.
Now, Express allows you to develop a web application,
server-side application, that will serve up content for
consumption by our frontend.
And Express provides a robust set of features, which we will
explore in more detail through the rest of the lessons in this course.
Express, itself, as I said, is a minimalist framework.
And Express also provides a way of extending and
adding functionality to Express through third-party middleware.
This extends the functionality of Express and adds in more features as required.
So you can build your web server using just as
many third-party middleware as required for
meeting the needs of your web server that you are designing.
We'll look at some examples of third-party middleware in this lesson.
And we will also explore more of these as we go through the rest of
the lessons in this course.
To use Express in your project, of course, the first step is to install Express.
And since Express is an Node module,
we install it by saying npm install express --save.
And this will install Express into your local project.
We will see the use of this in the exercise that follows this lecture.
Let's briefly talk about the Express middleware.
So what exactly is the purpose of middleware?
The middleware that Express supports provide a lot of plug-in
functionality that will be used to enhance your Express application.
Plug-in functionality, like for example,
we will look at a middleware called morgan, which allows you to print out
log information to the screen about the requests that come into your server.
Similarly, we look at another middleware called bodyParser,
which allows you to parse the body of the incoming HTTP request message and
extract information from it for use within your Express application.
We will see the use of these in the exercise that follows.
As I mentioned, morgan does logging of information to the console
on the server side, information about the incoming requests.
Similarly, we can serve up static web resources from
our server using the Express static.
So this will serve up information from a folder within our Express project.
And in declaring the project, you can say I'm w__filename and
__directory name or dirname, which gives you the full path for
the file or the directory for the current module.
And you will see me using that in the exercise.
Now that we have understood a little bit about Express and
the middleware that Express uses, let's look at a Node module.
Because this is the first time we're encountering a third-party Node module,
we'll look at some details about the third-party Node module.
So we'll examine the package.json file to see what is contained in
the package.json file.
We'll also look at semantic versioning.
So when you specify the version of the package that you use,
you always specify the version by specifying the <Major
version .< Minor Version >.< the Patch >.
So when you install a package, it is always identified by these three numbers,
major version which might introduce breaking changes.
So which means that, if you are installing a newer version of a package,
it may not be completely backward compatible with the previous version.
It may introduce breaking changes, whereby you may need to go back and
fix the code that you might have written in an earlier version of your project.
The minor version introduces some minor changes to your package and
may not be breaking changes.
A patch would be a bug fix that is often issued when a small bug is discovered.
So patches usually do not lead to any breaking changes.
And so you can easily use a higher patch version of a particular
package that you are using within your Node application.
When you are installing a package,
you can specify the exact version of the package to install by saying npm install.
For example,
if you want to install the 4.0.0 version of express you can say express@4.0.0.
So you are explicitly specifying which version of the package to install.
If you are okay with a higher level patch version,
you will say npm install express@"~4.0.0".
If a minor version, higher version of a package is acceptable,
then you will say hat and the name of the package.
Now this kind of information is also saved in the package.json file.
We will quickly pay a visit to the package.json file,
where you will notice some of this information being saved.
Now, when you do the exercise that follows this lecture,
you will see this in the package.json file.
This will be the result of completing the exercise that follows this lecture.
In the exercise, we will construct a simple web server using Express.
Now let me take you through a quick tour of package.json to illustrate some
information in package.json.
So in package.json file, you'll notice this information here,
this property for our JSON that is told here called dependencies.
The dependencies is where you will specify which additional third-party
modules on which this particular Node project is dependent upon.
So as you can see here,
we are saying this project is dependent upon express and morgan.
And note in particular how this information is specified here.
So this says hat 4.15.4, meaning that this will
work with any version that is 4.15.4 or higher.
You can use a higher level minor version and
this project will still be okay with it.
And similarly for morgan, we have specified the information here.
So, this additional information is added into
the package.json file whenever you do npm install and
say minus minus save flag for the npm install.
Now, also, you will notice that I created a .gitignore file here.
And inside the .gitignore file,
I specified that git should ignore the node_modules folder.
So what is exactly is contained in the node_modules folder?
If you install third-party modules into your Node application,
all these third-party modules will be saved in that node_modules folder here.
So within your project,
you will see that the node_modules folder has been created here.
And taking a look at the node_modules folder,
you will see a whole bunch of packages that have been installed.
Now, all these have been installed because you installed Express.
And Express in turn depends upon some other packages that are required.
So all those also get installed here by default.
In particular, let me also draw your attention to the express package here.
So if you go into the express package,
you will see additional information being stored in the express package.
So in the express package also, since Express itself is a Node module,
you will see a package.json file inside express also,
which contains additional information, which of course
is very detailed there and difficult for us to understand.
But note in particular that Express itself is
dependent upon many other Node modules here.
And that's the reason why all these other Node
modules also have been installed into the node_modules folder.
So when you install Express,
this will immediately also trigger all its dependencies also to be installed.
Because Express will require these other Node modules for it to do its work.
And also within Express, also, you see the index.js file.
So this is the starting point for our Express Node module itself.
And note in particular that the index.js file simply says
module.exports = require('./lib/expres');.
So the actual code for the Express module
itself is inside of this lib folder here.
And you can see the details.
So if you are curious to see the details of Express itself, you can go and
look in there.
But again, this may be a bit too much for you at this moment.
For the moment, just accept the fact that Express will do its work as expected.