backend, javascript, Node

Building My First App With Node

I’ve made it through the end of the first half of my programming intensive course at General Assembly. Just barely.

After a 3-week frontend introduction, we shifted gears to the backend. To do so, we downloaded something called Node.js. We also installed a bunch of “modules” onto Node to do specific things.

Traversing from the frontend to the backend
Traversing from the frontend to the backend of software development

I’m going to level with you: during my first week of learning about Node, I was completely lost. Like “I-have-no-effing-clue-what’s-going” lost. And it was particularly frustrating because I spent countless hours after class trying to understand what exactly Node was. I read a bunch of “intro” tutorials, watched at least 10 videos on YouTube, and even bought a few books on the basics of Node. Despite all my effort, I still couldn’t explain Node to my wife.

The tutorials, videos, and books presumed some basic knowledge of server-side applications. But I had no foundation to connect so many new concepts and terms. It reminded me of a recent passage by Tim Urban on his blog “Wait, but Why?“:

I’ve heard people compare knowledge of a topic to a tree. If you don’t fully get it, it’s like a tree in your head with no trunk—and without a trunk, when you learn something new about the topic—a new branch or leaf of the tree—there’s nothing for it to hang onto, so it just falls away. By clearing out fog all the way to the bottom, I build a tree trunk in my head, and from then on, all new information can hold on, which makes that topic forever more interesting and productive to learn about. And what I usually find is that so many of the topics I’ve pegged as “boring” in my head are actually just foggy to me.

So I set out to build a “tree trunk” and stick all these branches of knowledge together. I started at the most basic level by asking simple questions such as: “What’s a server?” Of course, I kind of knew what a server was, but “kind of” was not good enough.  I needed to understand how things worked at a more fundamental level. Admittedly, I was embarrassed at first to google these things or ask my instructors these questions during review sessions. Once I got over my fear of sounding ignorant, I started getting a much clearer and deeper picture of how the Internet and our computers work.

So, what is Node?

Node is a platform that extracts JavaScript out of its natural habitat – the browser (ie. Chrome or Firefox) – and provides a non-browser environment that JavaScript can run on. Through Node, we can access databases, listen to network traffic, and receive/respond to http requests – all with JavaScript. By embedding Google’s really fast and really awesome JavaScript engine onto our computers, Node allows us to build scalable network applications using solely the web’s lingua franca.

Since JavaScript is the only language I have been exposed to thus far, I initially failed to grasp the magnitude of Node’s impact. Before Node, a JavaScript programmer would have been confined to the frontend or would have had to learn a server-side language such as PHP, Python, or Ruby on Rails in order to connect to the backend and create a full-blown application. Thanks to Node, frontend developers can now work in the backend as well, and that’s pretty sweet since there’s now language consistency across the technology stack. More importantly, because of JavaScript’s asynchronous nature, we can write code that is non-blocking. In other words, a chunk of code doesn’t have to wait around for previous chunks of code to finish before it can start executing. Two or more chunks of code can run in parallel. The result: blazingly fast web applications.

An illustration of how Node's event loop enables non-blocking code
An illustration of how Node’s event loop enables non-blocking code

Using Node, I could create a chat server, an ad server, or a real-time data app. Indeed, using Node I was able to create my first application: a simple Wiki on startups. As a user, you can sign up and log in (I’ve encrypted passwords) as well as create, edit, and delete posts. Node is pretty low-level, so I also used a number of modules (such as Express and EJS) to build the server. Lastly, I hooked up to a Mongo database to store and retrieve information related to users and any posts on startups created.

I tried to go for a minimalist style that should make it intuitive to navigate. Due to the time constraint, I wasn’t able to build out all the functionality I wanted, but I’m still happy that I was able to build this Wiki over the course of a weekend. As simple as this application may be, the feeling of creating something from scratch is second to none.