All right.
And up next, let's set up a basic express server and let's also implement a Node mon package.
Now, if you're comfortable with both of these libraries, you can stop the video and set it up yourself.
Essentially, we want to set up a server on Port 5100.
Now, the reason why I don't go with 5000 is because lately on a mac.
There is an error since by default, the operating system is using that port.
Again, if that's not the case, of course, feel free to use a different port.
But in my case, I'm going to use 5100.
And essentially we just want to set up a basic home route which sends back Hello.
Now, if you're not familiar with Express.js, it's the most popular package to create servers.
In Node.
Yes, there are new packages popping up all the time, but Express is most likely one of the oldest
ones and it's the most reliable one since it has been around for so long.
And when it comes to Node one, essentially this package just spins up the server automatically for
us.
So we don't need to do it manually like we did in the previous videos.
Remember when we changed something we needed to start the server again?
So the Node one is going to do that for us.
I will showcase that in Node 18.
There's actually a built in way how we can do that, but that is coming up and throughout the course
I will still use the Node one as a node in I believe the last video I was removing the files and by
mistake I removed the server.
My bad.
So server is the one that we keep.
And those test ones, those are the ones we want to remove.
Now if you want to start working with both of these packages again, npm I.
Express and the node mode.
And essentially we want to create a most basic server.
So let's navigate to server.
JS We want to import Express.
From Express.js.
Basically from our package.
Now, if we're importing from node modules, basically from libraries, then of course we don't need
to add that.
JS It's only for our files.
And then we go with const app and we need to invoke it here.
So we go with Express and then we invoke it.
And like I said, the most basic home route essentially is going to be app dot, and then we can use
the methods.
So if it's a post, we go with App.post.
If it's a patch, then we go with app dot patch.
And of course we also have App.get, which essentially is going to respond to the get request and same
deal like we did in the client.
Remember when we were setting up the landing page we went with Forward Slash.
That was our home route.
Same deal over here.
So depending on domain, which in our case is going to be Port 5100, well that's going to be our home
page.
And in here, let's pass in the controller, which essentially is responsible for handling those requests.
And controllers by default have access to to objects, req and res and effectively we just want to go
with arrays, dot send and what do we want to send back is the hello world.
Let's save this.
And now let's set up App.listen.
So we need to provide a port where our server is going to be located.
So we're going to go with App.listen.
That's the method.
Like I said, in my case, I'm going to go with 5100.
Again, we provide a callback function over here and then in the console.
We're going to go server running and if you want, you can provide a port.
But since eventually we will set up a variable, I'm not going to do that right now, so I'm just going
to say dot, dot, dot.
And again, we can go with Node and then the server again.
But a better option is to set up a script and set up a node package.
And as a result, every time we make some changes in the server node, one is going to restart our server.
So let's navigate to Package.json.
We already have set up project and I'm going to go with the dev one.
So npm run Dev.
And in here, let's go with node one.
So that's the package and which file we want to watch.
Well we'll go with server and J.
So again, in order to spin everything up, we just want to go with NPM run dev, just like we were
doing on a client and check it out.
We have server running dot dot dot and if we navigate to our browser more specifically, we're looking
for 5100.
And remember by default browsers perform what a get request that is correct.
And check it out.
Now I do need to zoom in probably just so you can see.
So that is going to be my hello world.
And if you see the same result, we are in good shape and we can move on to the next step.