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
code 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 front-end 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 the 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 Web 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 front-end.
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're 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 a Node module,
we install it by saying npm install express --save
and this would 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 would 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'll 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'll 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, they 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
double underscore filename and double underscore 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 modules,
we'll look at some details about the third party Node modules so if 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 package it may not be
completely backward compatible with previous versions.
It may introduce breaking changes whereby you may need to go back and fix
the code that you might have written in the 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 then a small bug is discovered.
So patches usually do not lead to any breaking changes and so you can easily use
a higher version or a higher patch version of
a particular package that you're using within your Node application.
When you're 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're okay with a higher level patch version,
you would say npm install express@"~4.0.0 If
a minor version higher version of a package is
acceptable then you would say @ and the name of the package.
Now this kind of information is also saved in the package.json file,
we'll quickly pay a visit to the package of 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 stored 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 that this project is dependent upon Express
and Morgan and note in particular how this information is specified here.
So this is ^4.16.3 meaning that this will
work with any version that is 4.16.3 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 --save flag for the npm install.
Now, also you will notice that I have created a.gitignore file here and inside
the.gitignore file I have specified that git should ignore the Node modules folder.
So what 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 the 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 of these have been installed because you
install 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 index perspective 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,
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.
Also, within Express also you'll 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 express.
So the actual code for the Express module itself is inside this lib project,
lib folder here and you can see the detail.
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.
But I thought that it will be a interesting experience to visit the Node module's folder
to look at a structure of one particular third-party Node module in a bit more detail.
Also, you will notice that there is another file
here called package-lock.json that is installed.
Now this is being installed by the newer versions of npm.
The package-lock.json file is automatically generated by
npm which stores information about
the exact tree that was generated when you install
other Node modules and this is very useful when you
need to do installation of the Node modules at another location.
So for example if you download a Git repository and
try to recreate this project on another computer,
you would simply type npm install on the prompt and
that'll prompt your Node application to
automatically install everything that is specified in the dependencies here for you.
While creating that the package-lock.json stores additional information that is used by
npm to do the correct installation of all the npm modules that are required.
Now for the moment you don't need to worry about the details of
what is there inside the package-lock.json file.
With this, we complete this lesson where we have examined some details about Express.