Let's get started with Express Server.
That is how we can create server using express.
Well, to better understand express server or how to create server with express, I advise you to watch
the video and Node.js web server.
In that way, you better understand why Express and why Express is so fantastic.
So again, we have also explained what is a server.
So let's go ahead and get started with Express Server.
All right.
So the format that we are going to use is what you are going to use any time you want to write or develop
API or project using Node.js.
So I'm going to take my time to explain every step that you're going to go through.
All right.
So the first step is, as usual, we need to create a folder for your project.
So for this, because I am just practicing around with Express Server, I'm going to make use of I'm
k d I r mean, I'm going to create a folder called Express Server.
You can name it the name of your project.
The choice is yours.
So let me hit enter and let me see the end to the express server and let me open using my preferred
code editor.
That is Visual Studio code.
So now let's get started with Express.
The first step is we need to install experts.
So let's go to the official website for Express that is Express JS dot com and you can see that it is
fast and opinionated and minimalist web framework for Node.js.
So it takes away all the abstractions away from us to make it pretty easy to write Node.js code, to
develop APIs.
And also Express is being hosted on NPM.
So you can find Express on NPM.
So if it's such for X press.
Express NPM.
Let's see and let's start for the first one, Express and PM and let's see the number of downloads and
more details about experts.
Liquid say that weekly download is around 24 million per week.
How awesome this framework is.
So before we can install Express, we need to do a couple of steps here.
The first step is any time you start developing Node.js application, this is what you are going to
do.
The first step is we need to initialize our project as PM, meaning that we are going to use NPM packages
in our projects, for example.
Xpress Right, so let's go ahead and do that.
So first step is NPM init, meaning NPM initialize.
If we want to know more about NPM init, we can type NPM help and then init and let's see more details
about that command it is used to create.
What package JSON file do you remember on the web server of Node.js?
We talk about this file and here again and description it can use to create or set up a new or existing
NPM package.
And the initialize in this case is an NPM package name, create and initialize it.
So this is how we are going to use let me press control c to create.
Now let me issue the command again and p m init so again npm initialize so hit enter.
Now NPM is asking us a couple of questions about our project.
The first question is what package name, meaning the name of the folder that we want to use because
we have specified express folder.
That's why by default it has selected or we can override it, let's say my API or my server, but I
like it in this way, so I'm going to hit enter.
The next step is the version of my project.
So let's see.
For the first time the rest of my project is going to be as 2.1.1 it's up to you hit enter and here
give description about your projects.
Let's say that creating.
Express.
Sarah.
The next is entry point, which means that what going to be the main file name that we want to use,
like I told you and that Node.js server creation that for backend development or for main files or back
end development, we often use index or app or server, but I prefer to use server JS so I'm going to
override this default configuration and hit enter.
The next is test command, meaning a command for tests in your project.
In our case, we are not going to do any test case, so I'll hit enter and then git repository tool
for the meantime to let's ignore this one.
And now the keyword for H2 purposes, let's say just this one for zero purposes.
The author is going to be the name the developer.
I know what academy and the last is hit enter the line space by default choosing IAC and let me hit
enter and now ask me is this okay?
Why not then our press okay or enter?
Now we see I have object with properties if I check the file this what I have and now these are the
configuration I use inside that and all this name can be overridden, meaning we can change the property
names here.
For example, the name of my folder, the version and description, and I see that.
So and the key words here, you see we are specified here and the author has that now we are done with
the configuration.
The next step is we need to install express as we saw on the page.
So for installation of packages, make sure that you install the exact version as I'm going to install
in that way.
That is, in future your code would be the same as my because I used to have this kind of problem with
my students.
Maybe at the time of recording this video I'm going to use Express version point four, maybe after
three years, four years to be an update.
In that way, the code base will change a little bit.
In that case, your code and my code will never be the same.
So to avoid this, make sure that you install the exact version as I'm going to install.
And if you understand version one of any package for any update, you can play around with it and know
how it works.
So let's see the version of NPM at the time of recording this video, which is 4.1, 8.1.
So let's go ahead and install that.
So here we go.
MP Am I going to be X Press at 4.1, 8.1.
Let's confirm if it is true.
Yes.
So now let's hit Enter.
Now Express is installing the packages for me, so let's have some patience now.
Indeed, Express has finished.
When I open my project I see a folder code.
Not this one too.
We have talked about it before, so this folder contains all the dependencies that our application need.
For the meantime, we have installed experts and we have this bunch of folders.
So these folders or this files are all needed by express to make our life easier.
So we are not going to touch the Node.js module.
Then this file is going to be packaged.
That locked JSON file is because I am using two package managers, for example John and PM.
That's why I have this.
But in your case, if you are using only one John or PM, you are not going to see this one.
So let me delete this one and that is it.
Next is we need to create our file.
Remember that inside the package we specify that the main file is supposed to be server, so we need
to create the exact file going to be server.
Sarah Dodd.
And now the first step is we need to require experts.
So here comes remember and import and export required then x press.
So let's console.log x price and see what express has for us.
So back to my terminal here.
Let me click the console and run the application again.
Right.
We're going to be node going to be server dogs or server.
And you can see again, we have a bunch of methods.
The express is going to make our life easier and we have use for injecting middleware, route engine
for rendering, template engine, the path and many more.
So let's go ahead and create a server with Express.
So we are going to create instance of Express as an app and then we assign to express as that meaning
that express is a function and then we assign the result coming back and assigned a variable called
app because functions are first class citizen, we can assign a variable to a function as this.
The next step is let's create server.
So let's see how we are going to create a server with express on the app.
We have a method called listen as we discussed before and you pass in a port, let's say for this one,
let's pass in 9000, then this one going to be a callback function and let me use normal function called
here as that.
And inside I'm going to say server is up and running and with few lines of code I'm able to create a
server.
All right, so let's run the application and let's see if our server is going to be connected.
And indeed, we see I see server is up and running.
Do you remember under Node.js how we able to create a server?
Let me show you the code for that.
So this is a code for creating a server in vanilla Node.js with some two lines of routing here.
But with Express, there is no routing here.
But instead I am able to run my server how easy it is.
All right.
So now that we're able to create our server now, we haven't done yet unless we respond to a request
through routing.
So in the next video, let's see how we can implement routing and express JS.