Hi Rafal,
I feel you, don’t worry client/server programming is hard to grasp initially! I’ll try to explain as much as a I can.
Your Glitch app (as basically any other webapp) is composed of two parts: a client and a server. The client is run by your browser when you load a page, the server is run… by the machine on which your Glitch app is hosted
server.js
is the entrypoint for the server-side of your web application. It is responsible for storing data you want to share among all the “clients” (that is, all the browsers that will connect to your application), and to serve (hence the name… server) the requests that the clients make.
So, you can see that server.js
defines a few “routes”: for example app.get("/", ...)
. Routes tell the server how to respond to client requests. So app.get("/", ...)
describes what to respond when a client asks for the /
path of your webapp: that is, it serves index.html
.
If you look in index.html
, you’ll see that one of the lines “includes” client.js
: so when a client (the browser) goes to https://your-app.glitch.me/, it also receives the “client-side” of your web application.
So server.js
and client.js
run on two completely separate machines, on two completely separate environments, and serve two completely separate uses: server.js
is responsible of responding to requests, while client.js
… makes the requests to the server, and shows them to the user through the browser!
So when in client.js
you see $.get("/dreams", ...)
, it’s the client-side that is asking to server.js
to serve the “route” /dreams
. If you look in server.js
, you’ll see what app.get("/dreams", ...)
does:
app.get("/dreams", function (request, response) {
response.send(dreams);
});
It sends the content of the dreams
array (defined at line 30 in server.js
) to the client. This is how the client knows what’s in your current dream list
I hope that this information is enough for you to get the rest: when the client does a $.post("/dreams")
, it is asking the server to respond through app.post("/dreams", ...)
, which adds a dream to the dream list (the dreams
) variable.
Let me know if you have any further questions, or if I just made things even more complicated!