Finally, the time has come for us to create our own web server using Node.js.
We are going to use the HTTP core module from Node.js to get this done.
So let's get back to our terminal.
The first step is we need to create a folder for this.
You can use the manual way.
That is right click in and the new folder and the name of your folder after that drag and drop inside
your Visual Studio code or your preferred text editor.
But I advise you to stick to the terminal in that way.
You'll be familiar with the command, so let's go.
The first command is to create a folder that is mkdir and the name of the folder I'm going to name it
as web server.
Now the folder has been created to make sure that it has been created, make use of L as meaning list.
And now you can see that I have the folder on my desktop called Web Server.
The next step is I need to change or CD in to this folder.
So CD and the name of the folder which is web server.
Now hit enter.
You can see that.
Now I am inside this folder.
The next step is to open this using the command code and then period.
This command works with all visual CD code users.
So let's hit enter now and we see that it has open for me nicely.
The next step is you are going to create a file for this and you can do it in two ways.
That is using the gooey or right clicking and the name of the file or you can use the terminal.
So I'm going to use the terminal to create the file.
So I'm going to make use of touch as a command for back end development, especially when you are creating
a server.
The name of the file we often use index or server or app, so let's use server dot js.
The name is up to you and we see that I have the server file being what created for me.
Now let's get rocking because we are going to use the HTTP call module to get it down.
Let's require the module.
The first step comes we're going to use HTTP equal to require the module.
That is the HTTP.
Now, here we go.
Now, if you try to console log the HTTP, you're going to see a bunch of methods that we have on it
to get the job done.
So now let me run this particular file.
So clear the console and I'm going to use Node, the name of the file which is server JS.
And you can see that we have a bunch of methods on that.
So now how can we create our first server?
Let's remove these comments.
Now, let's have a beautiful comment here and say it's create server.
And on the HTTP instance we have on that, we have a method called create server.
And this is a method for this method.
I'm going to pass in only one argument and that is callback.
We can use a function or the normal function syntax.
So let's go by the normal function syntax.
Inside the callback we have access to two things.
That is the request the user is trying to make and the response by convention.
We shorten it as our queue for requests and then our E is for response as that.
Now inside the callback, that is where we are going to write our logic for the server.
So let's have some steps here.
Step number one is we need to add what is called HTTP headers and I'll explain to you very soon after
that we need to send what is called the response header.
I will also explain to you very soon after that we need to send data to the user, send data to the
user.
And then last step is we need to end the response.
Great.
So what is it?
HTTP header at http headers simply means that it gives more information about the requests and the response.
And also we can also pass a data into the headers.
For example, if you get back to authentication, you often use or pass in a token to the headers.
So for our case, you are going to stick to the response header for now.
So for the response header, what we are going to do is that we need to tell the user that his or her
request was successful or not, and we can do so by writing a header.
In this way we're going to make use of write head as that.
And this right here we pass in two arguments.
The first one is what you call the status code.
One is the status code.
It gives more details or information about the request, whether it was successful or error or code.
So we will dive you more to show you types of status code.
For the meantime, let's stick to 200.
We simply means okay.
If everything is okay, it means that we are going to send the status code called 200.
The next agreement is what you call key value per.
So it goes like this as an object.
So for this we are going to tell the server or the response, the kind of data that it needs to send
to the user, and we call it as content type and pass in as a JSON content and then type and then semicolon
and then the value that must be in double code because this is a decent format.
Next is what kind of data do we want to send to the user?
If we go back to API development, we are going to retain what is called JSON data.
But for our case, let's just send a plain text, going to be text slash plane.
Now this line of code gives more information about the response and most importantly, we are telling
the server, please, this is a kind of data that we need to send to the user.
That's the first line of code.
The next step is definitely we need to send something to the user.
If the user tries to visit our endpoint to have access to our server and are going to use response dot
right.
And this is a function and here you pass in the data that you want to send to the user.
For the meantime, let's just say that hello.
This is my first web server.
Perfect.
So all what our server is doing is to send this data called.
Hello, this is my first server.
And lastly is we need to end the response.
So on the response, we have what is called dot ent, meaning we have ended the response.
The next step is if you try to run the server or a file, nothing seems to be happening.
So note I'm using up our key to navigate through my history.
Now I have node server the JS and you can see that nothing seems to be happening well.
So after this one we need to listen to the server because a server must be always 24 seven.
So what comes back from this is what we call the actual server and it name can be anything.
We are assigning the response coming back from this to what is called a server.
And if you try to console log server, let's see what we have on it.
Now back to my terminal here.
Let's run the application and you can see that we have a bunch of methods on that and what we want is
what we call listen.
So on the server that we got.
So let's have a beautiful comment here and says listen to the server.
So on the server we have a method called lesson and this lesson takes into arguments.
The first one is what we call the port.
Well, a port is a number on which the server will listen to, or technically a port is a number assigned
to uniquely identify a connection endpoint and to direct data to a specific service.
So a port is a number of which our server is going to listen to.
So I'm going to pass in here a number and it can be anything but make sure that the number or the port
is going to be used by different application.
So I will show you how in case I'm going to use a port that has been used by a different application
on your system.
So most at times make use of 80, 80 or let's say 5000 or 7000 or 9000.
So let's use 7000 for this purpose.
The next step is we pass in a callback function to tell the user that, Hey.
The server is up and running.
Let's use our function.
This time around we can use normal function.
So inside a callback when our server get connected, we want to send this message since that server
is up and running.
All right, so moment of truth.
Now let's try to run the server again.
Make means of node and then the server.
And now I told you, you see something here and we are going to see this kind of message a lot, which
is a D in use.
It simply means that the address already in use, meaning this part that you specify is being used by
different application or my operating system.
So anytime you see this kind of message, it tells you that the part you are using is already being
used.
So let's change the port.
Let's make it 9000.
So moment of truth, let's clear the console.
Now let's run application and now you can see that server is up and running.
So how can a user access our server?
Definitely the user need to have a euro, but because we have not deployed this particular server or
application, we are going to have access to what is called local host.
So now the you are or the endpoint is going to be at HTTP.
Let me have a comment at HTTP semicolon for s large voice slash local.
Host and then semicolon, the name of our port which is 9000, which is this.
So what is a look?
Host In computer networking localhost is a host name that refers to the current device used to access
it.
Since we have not deployed the application, we are going to use the localhost, which is the host name
that refers to the current device.
That is my operating system or my machine.
So if I copy this, you are error and now I open my browser and paste it and hit enter.
Now if we see that, we have something here.
Hello.
This is my first web server.
You can see we have the response back.
Hello.
This is my first web server.
But let's change the kind of data we are returning back.
So here instead of playing, let's make it a HTML.
We are returning a HTML for the meantime.
Now, any time you make changes to the server, we need to restart it.
So now back to my terminal.
Let's shut down the server and then run again.
Now let's refresh the page.
Now, if we see that we have the same text, that is.
Hello, this is my first web server.
So a takeaway here is that any time a user visits our URL, any function in here gets called.
So any function we put in here is going to be called.
So let's try to console.log and let's say that someone.
Has visited.
The.
Simple as that.
So now let's restart the server because I've made changes to the file and now let's run the application.
Now check the terminal.
It could see that someone has visited the server.
Let's go through what is going on here.
Any time a user visits our you are all the code inside the server is going to run.
So first of all, it gets here and says, okay, now I'm going to send status code 200, meaning everything
is okay.
And here the user is requesting for a plaintext, which is a HTML and line about hand.
Here we are sending the data to the user and after sending the data we need to end the response here.
That is the basic flow of a server.
In the next video, we will talk about how we can render a HTML page using our server.