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.
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.
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. Once we get familiar with them and just getting a bit more confident, we learn about Express.js 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)
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.
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.
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. 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 which is an npm package to make our lives easier. We can now create databases and tables in those databases, and seed them (fill them) with data.
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".
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 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