After the problems I was having with understanding the structure of Node and its frameworks (https, express etc) I picked up the The Node Beginner Book by Manuel Kiessling and read it over the weekend.
The book does a good job in building concepts from scratch, particularly arguing the benefits of Javascript and the benefits of having Node a server side language that helps you configure https servers (a lot of data handling can be organized better with the help of callback functions).
So we went about building a basic HTTPS server in a file called server.js.
Ahh! We have used the https framework written in Javascript as a library in Node to call a function “createServer” which has a function with an object called “request” that listens to URL request received from the browser. The “response” object calls internal functions like .writeHead and .write to send data back to the browser page (in this case “Hello World”). createServer also knows that it needs to listen to a specific port “8888”.
I won’t go through the entire tutorial here, but the next step is important, introducing call back functions,
Here’s the explanation with it,
“What we are doing here is, we pass the function say as the first parameter to the execute function. Not the return value of say, but say itself! Thus, say becomes the local variable someFunction within exe- cute, and execute can call the function in this variable by issuing someFunction() (adding brackets).
Of course, because say takes one parameter, execute can pass such a parameter when calling someFunction.
We can, as we just did, pass a function as a parameter to another function by its name. But we don’t have to take this indirection of first defining, then passing it - we can define and pass a function as a parameter to another function in-place.”
“We define the function we want to pass to execute right there at the place where execute expects its first parameter. This way, we don’t even need to give the function a name, which is why this is called an anonymous function.”
Fun.
My life got pretty exciting after that. I’m not even sure if I’m being sarcastic, lol, but one thing’s for sure I am currently living in Callback Hell. Going through the book hasn’t been the smoothest undertaking, but I feel I’m getting somewhere. When I think about it in the context of my finals, I panic I’m not!