In the previous lecture,
we learned about the essentials of networking including details of the HTTP protocol.
Now, we'll look at how Node can be used to construct an HTTP server.
This is where the core HTTP module that is built into Node comes to our aid.
In addition, we will look at a couple of other core Node modules that are useful for
building an HTTP server from using Node.
So, how do we make use of the Node HTTP module?
As I mentioned, the HTTP module is one of the core modules that are built into the Node.
So, this is a core networking module that supports
a high-performance foundation for HTTP stack.
So using the module within our Node application requires
us to import the module into our application.
So this is what we do by saying const HTTP = require and,
the name of the module HTTP there.
Now, once the module is imported,
then this HTTP module supports a createServer function which takes,
as a parameter, a function that acts as
a callback function for the createServer module and
this function has two parameters : request and response.
The request is the request message that comes in from
the client side and we can parse the request message and then extract
a lot of information from the request message and use it to make
decisions on how we construct the response message corresponding to that.
The response message is constructed using the second parameter here, the res,
on which we can construct the various header values for
our HTTP response message and also the body of the HTTP response message.
Now, to start the server,
once you create the server,
then you would say server.listen and then supply the port number
and the host name for the server and that will start up our server.
We'll look at some details in the examples exercise that follows this lecture.
As I mentioned, the incoming request message information is available
through the req parameter that the function takes in the createServer method.
So, the req or request,
if you want to give it full name,
you can call it as a request but in general we have found that in
examples we used req to represent the request message.
It supports on the Javascript object properties like the header,
the body and also various information that can be extracted,
the URL and the method that has been requested by that client site.
And, in response, you construct the response using
the response Javascript object which supports that setHeader function,
the statusCode function which can be set to the status code of the response message.
And then you would write the message saying
res.write and you would end the message by saying
res.end and that takes the final part of the body for the message.
The body may be either standard text or HTML or
any other information that you want to enclose inside the body of that reply message.
But once the res.end is called,
the reply is sent back to the client from the HTTP server.
Two other core modules that are useful for us
when we're constructing the Node HTTP server is the Node path module.
The path module enables us to specify the path to a file and then
examine whether the file exists or examine more details about a file.
For example, the extension of the file,
name and so on.
So, the path module can be used within
our application by requiring the path module as shown here.
And then, this supports methods,
like for example, path.resolve,
which will convert a relative path into
an absolute path that the entire path to the file.
You can also check the extension name for the file by calling the
path.extname function and supply the file path that will
examine the extension of the filename.
Similarly, the file system module,
again a core module in Node,
will enable us to read and write
files that exist in the local file system on the computer.
So the file system modules can be used within
our Node application by requiring it as shown here.
And then, these file system modules supports many methods
including a method for checking whether the file exists in the local file system or not.
So the fs.exists will take
the filePath as the first parameter and the second parameter is
a callback function which will come back with a parameter which we can name it as exists.
This parameter, exists, will be true if the file
exists and will be false if the file does not exist within the local file system.
Similarly, you can read from the file by creating a read stream given the file path.
So you can say fs.createReadStream and
the filePath and this can be piped in to the response message.
So the file will be read in from the file
given by the filePath and then the contents will be put
into the body of the response message by calling a chained function as shown here.
So, these are some examples of some core Node modules that will be very
useful when we are constructing a HTTP server using Node.
Now, that we understand some details about the HTTP server and how it can be constructed,
let's go to the exercise,
where we will construct a simple Node HTTP server which first will serve up
some basic information and then after that we'll serve up files
that exist in a public folder in our project.