As I mentioned before, developers are really, really good and making sure they don't keep doing the
same thing over and over, and if there's anything that is done over and over, they build a tool for
it so that it becomes simpler.
Well, building servers was one of that, it's something that everybody uses.
So people use libraries just like we used to react on the front end on top of JavaScript to make our
lives easier and build faster, more robust servers.
There are many libraries that people use with note to create a server, but the most popular hands down,
the best one right now is Express.
And I have here the state of JavaScript.
Servais It's a famous survey that gets done every year at the end of the year that asks developers what
their favorite tools are, what front and tools they use.
And I highly recommend that you check it out.
But he can see here for the back end, the library that is most popular is Xpress.
And this purple bar means I've used it before and would use it again.
There's other ones like Koha Media are happy, but the usage is very, very small.
Express's just a tried and tested.
There's plenty of tools and information online for it.
So if you ever get stuck, you're able to find an answer to your issue right away.
So expresses the way to go.
So let's learn some express and show you how much easier it becomes to create our own server.
So we're going to delete the server that we built previously in the last video and install Xpress,
let's do NPM install and again through NPM we have the express package.
And now we have express available to us, so let's grab it that way, you created an express server
is very simple.
You simply do express require.
Express, we have to do a concert app and then to.
Create that app to run express, we simply do express.
And then finally abduct listen.
And that's it, we have express running.
Let me do npm start.
And we see that our server is running.
And if I go back to localhost three thousand.
I have cannot get and that is because, well, again, we're not doing anything, but in just three
lines we have a server and I want to show you that Express has great.
A great guide to getting started and a ton of information of how to use it.
And documentation for you that you can check out on your own, but for now, let's build our own.
Well, Express's really, really good at doing around it, if you remember, in our localhost, we got
cannot get.
To do a get request with Express, we simply do not get in the first parameter is the path.
We'll just do the route path.
And the second parameter receives a request and a response and an express, instead of writing everything
out, the common way of doing it is doing Rech and wrests.
And in here.
We can say response that sent.
And we'll just send a hello, just like we did in our previous video, where we use the HTTP module
that comes with node.
So let's check that, let's see if that works.
I'm going to save.
And I get hello.
If I open up the console here and the network tab and I refresh again.
I see that the response.
Yes, hello.
We see content type.
Textile mill that was converted automatically for us.
I can also, instead of send just an e-mail.
So instead of doing something like H1.
And each one.
Which gives us an H1 hello.
I can also now send Jason.
Const user again, and this time the user name will be Sally.
Will be soccer.
And I need to add an equal here and then in user.
If I save that and go back and refresh.
Look at that, it automatically does the JSON string if for me and returns a JSON content type, an
application JSON content type with the response that is Jason.
Very cool, so you can see the power of Express, even just from the simple example, it takes away
a lot of the repetition and makes our life a whole lot easier.
Now, we've only done get here, but you can also do post requests.
So, for example, if I did post profile, well, now if I refresh.
I get an error because.
Because now I've made the request method, which was get from the Bouser, but here I only.
Accept a post request.
From Profar, if I do.
I get cannot get profiled because even though we are going to the proper you, Earl, now we're doing
a get request.
Any time we refresh your browser, it's a get request.
And in order to do that, we need to actually send some data over.
And I'll show you how to do that in the next couple of videos.
But just to show you how Express works, I can keep adding these routes less to get a profile that returns.
Getting profile.
And if I do a.
Another get this time to the root, Yoro, and this one, I'll just say getting.
If I say this now and refresh profile.
I'll get a two hundred, OK, with the response of getting Profar.
And if I do no profile and just the URL, I get root.
And the way Express works is it checks each one line by line.
So, for example, it will check, hey, did the request have the route URL, did the get request,
have profile, did the post request, have profile?
And if it matches any of these.
Well, it will run whatever the function inside of it is.
If I do a U.
RL that doesn't exist.
I get a four or four not fund because, well.
We did a get request to this URL that doesn't exist.
So using this, we can use the get method, the post method, the put method and the delete method that
we're going to actually demonstrate in the next couple of videos.
But as you can see, it's a simple matter of saying if you go to this route, do this, if you go to
this one, do this and everything is just logical and trickles all the way down.
All right, I'll see in the next video, bye bye.