Hello! Today we're going to talk about servers. So, what are they? Well if you're familiar with the industry and/or play(ed) videogames in today's world, then 9 times out of 10 you've complained about servers being bad at some point. Turns out, I'd been complaining about them for years without knowing what it technically does, until now. So, let's dive in.
Let's say we are heading to a famous restaurant together. We can imagine that this famous restaurant is renowned for it's excellent service. However, once we arrive there, we find out that the wait time is a bit longer than we'd like it to be. More time passes, and we are finally treated to our table and begin considering our order. Eventually, after 15 minutes, we figure out what we want and are now looking forward to making our order. Suddenly, another 30 minutes passes by and we have yet to make our order. Our waiter finally arrives, but his attitude isn't the best. We order our food with him, and he sends our order to the back of the kitchen. Now, imagine if it takes another hour+ JUST for your food to be made? I'm sure by then, you'd have the urge to walk out, if you haven't already.
In this example, we can confidently say that the service wasn't the best. Not only did we have to wait a while coming in, we also had to wait another hour once the order was made. So even though the restaurant was packed, it still doesn't change the fact that your personal experience wasn't the best. And knowing how popular they are for having great service, it only amplifies your own disappointment.
Keeping this example in mind, "servers" in our code, act as our "waiter" when dealing with specific data. More specifically, Servers are used to fetch information that's requested from the client. They are also responsible for saving requested information to a specific database(aka kitchen)!
So, how do you make a server as a developer? Well we can choose to use any server-side framework for our implementation. To be frank, there are plenty of different server-side frameworks out there. For the purpose of this blog however, we're going to choose one of the most popular ones that most agree is fairly easy to learn: Express.js.
In order to install the packages for our server-side framework, we must first add it's dependencies to our package.json file. Also, in order to start our server, we must add a "start" script to our package.json that points to our server file. From there, assuming we're using node, we can install our dependencies and run the command npm start in our terminal that will essentially "start-up" our server later on.
Now that we've installed, we can navigate to our server file.
Here is where you will be using express to get your server to respond. In order to use express, we must first "require" it by saving it to a variable. We then create a new variable that would invoke that variable pointing to our require. Like so:
Once that is done, our server now needs a port number, or address in a sense, to run our application on. Thanks to Express.js, we can create it like any other variable, and assign it to a number. From there, we can use the built in "listen" method that express offers to listen for any changes or visitors to our app at a specific port location. Like so:
So now, when we start our server in the terminal, your terminal should ideally respond with a message letting you know that it is working and can be visited at a specific port number.
One thing I want to point out, is that our port number can technically be any number. In most cases, a common port number you'll see in development is the number 8080. From that point, we would use our server to help perform actions requested from the client using middleware. Other than that, that is a quick intro into servers!
Top comments (0)