But What On Earth Is A Server? And What Is API?
Tags: career, javascript, node, server
Published: 2020-03-02 08:56:39


The second block of my journey is three weeks on Backend.

"What is Backend?" - I hear you ask. "Some servery based stuff that happens in the background but I don't even know what a server is." - I would have answered you three weeks ago.

# HTTP Requests
First we learn the basics of how the internet works.
>The internet is a global network of computers that works much like the postal system, only at sub-second speeds. Just as the postal service enables people to send one another envelopes containing messages, the internet enables computers to send one another small packets of digital data.

(Thanks [BBC](http://www.bbc.co.uk/webwise/guides/what-is-the-internet).)

Computer #1 sends a request for some data to computer #2 and computer #2 sends a response back.
We learn about http requests and how to make them in [Node.js](https://nodejs.org/en/). Once we get familiar with them and just getting a bit more confident, we learn about [Express.js](https://expressjs.com/) which makes things a million times easier.

Have you ever wondered what all that gibberish is in your url with question marks and equal signs? They are _queries_. And we learn about them too.
(`https://dev.to/search?q=banana` - the 'q=banana' here)

We also learn about a lot of fancy-sounding things like middleware functions and routers, the model-view-controller a.k.a. MVC pattern. In terms of Javascript, we get to understand and use Promises. A lot.

So we can now send all sorts of requests to servers that we still don't understand but they are good at sending us the stuff we are requesting. We are sending these requests to third-party APIs.

To what?

# API
API stands for Application Programming Interface which equally doesn't help to understand what they actually are. I am going to try and explain it through an example of ordering food (in a simplified way).

It's a Sunday evening and I can't bother with cooking. I grab the menu of my favourite pizza place off the fridge and browse through the menu. Medium size pepperoni pizza with extra cheese and a can of drink.
- I place my order to the restaurant (_send my http request_)...
- ...who will receive my order, step-by-step put it together (make the dough, pre-heat the oven, add the right toppings, add the extra topping, put the pizza in the oven, bake it, box it, get my drink out of the fridge) and send their delivery guy. Boom! I got my pizza (_response_) within half an hour (in milliseconds).

My boyfriend just gets home and, seeing my happy face, also wants to order a takeaway but he wants to eat Indian. Chicken korma with peshwari naan and some onion bhajis. But how will he know if that's possible to order? He can't look at my menu, that would cause some confusion for sure. He will have to use a different menu.

So an API is like a menu. A computer's API specifies how _my_ computer can interact with it just like a menu tells me what I can order from a restaurant.

With this, I have now explained what a GET request is but there are also other methods such as POST, PUT/PATCH and DELETE which will not fit so well with my food analogy. Posting would be equivalent to something on the line of sending a new ingredient to the kitchen, putting/patching would be to change such ingredient and deleting it would be to make them put it in the bin.
A much more reasonable example is let's say when you send (_post_) a tweet on twitter which you can edit (_put/patch_) and also _delete_.

#Databases and SQL
We can also make our own database where we can _serve_ information from (getting to the server bit in a minute). Continuing with our takeaway, it's a bit like opening my own kitchen. I can have all the ingredients waiting to be made into a meal for delivery.

We learn SQL from [SQLBolt] (https://sqlbolt.com). It is a language used for creating and managing data held in a (relational) database. "What? Learning another language?" Yes. But it is actually fairly simple (at least at this stage). We also get introduced to Postgres which is a database management system and works pretty well with Node.js. We learn to use [Knex.js](http://knexjs.org/) which is an [npm package](https://www.npmjs.com/) to make our lives easier. We can now create databases and tables in those databases, and _seed_ them (fill them) with data.

#Server
We are finally getting here! It's time to build our own server. But what is a server?
>In computing, a server is a computer program or a device that provides functionality for other programs or devices, called "clients".

(Thanks [Wiki](https://en.wikipedia.org/wiki/Server_(computing)).)

The client is the lazy Sunday takeaway eater who orders from the takeaway place - in this case the one with the kitchen and all the ingredients is me.
Building a server is essentially building the API or creating my food menu.

When making a server, I can serve up either my own data from my database or - this is where it gets pretty cool - I can serve up data from another API!
Imagine another restaurant letting me use their ingredients to make my own food. And it's all cool to do so (I assume within certain rules of copyright).

Let's take this [Chuck Norris Database](http://www.icndb.com/) as an example. They store a number of Chuck Norris jokes somewhere in a database. I can send them a GET request as per their API. Let's say I want to get a random Chuck Norris joke. I can use the following _endpoint_: `https://api.icndb.com/jokes/random` which will send me back a random joke. This is because they wrote their API in a way that this endpoint will always _serve up_ a random joke. It's on the menu.

This opens a lot of possibilities to gather information from other servers to use on my own app. Think about any app that uses a map - they could use google's map data like magic.

Overall I can say I really enjoyed the backend block and for the first time since I started with coding I actually felt that I was doing something that I might be doing in real life rather than just solving puzzles. 

Cover Photo credit: [Jesús Terrés](https://unsplash.com/@nadaimporta)