the project using the editor Visual Studio Code in my case and then,
in the package.json file let's go in and add in the new script called start,
which is node index and save the changes.
Now, that we have initialized the node module,
let's go ahead and install the Express framework within
the project to make use of it to build our express server.
So, at the prompt,
type npm install express.
So this is how you would install
a third-party node module if you are taking this course for
the first time and have not taken the previous courses
then this is something that is important to note.
This is how you will install a node module by adding the minus minus save,
we are also saving the information that
this third-party node module is a dependency
for our project and this information will be saved in the package.json file.
If you have taken the previous courses of the specialization,
then you already know about this information.
So, let's go ahead and install the Express module.
As you can notice,
we are using Express version 4.16.3 in this course.
So, let's now go back to our code.
Taking a look at the package.json file,
you see that within the package.json file by installing Express we already see
another property that is included into the package.json file called dependencies.
This dependencies track all the packages
on which this current project is going to be dependent upon.
Here you see Express being installed
there and the corresponding version of Express being installed here,
so this declares that this particular project is
dependent upon Express version at least 4.16.3.
In addition, you also see this folder called node modules created here.
So this is where all the third-party node modules that are installed by
your application are going to be stored.
Now, when we do a Git commit,
we don't want to commit this node modules folder.
So to ignore that,
lets create a file
named.gitignore and within the.gitignore file,
let's type in node_ modules.
So, this is letting Git know that we do not wish to
commit the node modules folder to the Git repository.
So, with this let's save the changes.
So now when you create a Git repository,
the node modules folder will be ignored.
It is not required because you can always recreate the node modules folder anytime
you clone this project from your Git repository by simply typing npm install.
This will look at the package.json file,
and look at all the dependencies,
and the development dependencies that are in the package.json
file and are automatically installed all those packages for you.
Now, that we have completed this,
let's create our first Express project,
so to do that we'll create a file named index.js and in the index.js
we'll declare a const express require express.
Now, you'll note that express is a third-party node module and it has been
installed in our node modules folder in the correct directory.
So, when we declare this here saying that this Express module is required,
then it will be automatically included from the node modules folder into our application.
Now, also install HTTP
the core module here.
Now, that we have installed this,
let's create the course name as
local host and port number as 3000.
So you see that we are using exactly the same way of doing
this exercise as we did for the HTTP exercise earlier.
Now, at this point,
we will declare this const called app as express.
So this way, we are saying that our application is going to use the Express node module.
So, once we do that,
then Express provides a bunch of methods that we can use to construct our web server.
So after this, we'll say app.use() and inside here,
we will declare a function that will be called to set up our server.
So, this function takes three parameters req,
which is the request; res,
which is the response, and next.
Now, as we saw Express uses additional middleware.
So, the next is used when you need to
invoke additional middleware to take care of work on your behalf.
We will see the use of next in some of the later exercises,
but the function here will take three parameters req, res, and next.
Next is an optional parameter that can
be not included if you are not going to use it within your code.
So, inside here we'll say,
console log and then,
we'll log that headers,
and we'll respond with the status code set to 200.