Node.js - First foray

Elijah Samuels
5 min readJul 23, 2021

--

I have something to admit: I like JavaScript more than other languages. Ruby was really cool, and it’ll always have a place in my heart, but man… JS seems to have so many more applications (pun intended!), and the versatility seems to be pretty much limitless. Whether you’re doing frontend or backend, JavaScript will say, “I got you!”

Coming out of bootcamp, we primarily utilized JS, and the library, React, for frontend usage. Get/Put that data from our Ruby on Rails backend and API, but use JS and React to view that data. And while it’s a great way to do it, I always felt like, if we could be focusing, and really learning JS and more of it’s uses, we’d really have something. I compare it to my musical studies in woodwinds: If I had spent my time on just saxophone instead of spreading it out to include flute, clarinet, oboe, bassoon, and some piano, I could have been an even better saxophonist. Had we spent all our time focusing on just JS, we’d be even better JS developers! As you can imagine, there’s always a trade off, and this one being we wouldn’t have been as well versed to learn other languages. Alas, here we are, and Node.js has piqued my interest…

I’ve only just begun to start with Node.js as a backend, but it’s already pretty cool. I wanted to document it here, to help solidify this knowledge.

What kind of projects is Node good for?

  • Basically anything that isn’t super demanding of the processor.
  • Chat apps
  • Live Updates
  • Real Time services
  • REST API
  • CRUD (Create Read Update Delete) apps like blogs or social media

Databases?

You can use relational databases or non-relational database like MongoDB. I haven’t played around with Mongo yet, but the idea of having everything in one giant JSON file seems really cool, but also somewhat odd.

Setup Steps

There’s a strong likelihood you already have node on your machine. In your console, type “node -v” and if you get some numbers, yay! You have node installed.

If you need to install node, it’s super easy! You can either download directly from nodejs.org site or use npm to install it with this command, “npm i node”

Side note: npmjs.com has all the terminal commands (upper right) you need to quickly install just about anything!

Now that we have node working on the machine, let’s play around and spin up a basic (local) server.

  • Start by creating a file titled, “server.js”

In this file, we’re going to bring in the http module by requiring it and setting it to a variable called “http”

Next, we’ll set our HOSTNAME and PORT. In both instances here, each variable will return it’s assignment after the OR || operator.

Fun side note: if you add, you’ll see a bunch of cool info. Try it!

  • console.log(process)
  • console.log(process.env)

When working with JavaScript in a browser situation, we have access to the window or document. That’s not the case in node because there isn’t a window object. Instead, we have the “global” object and the “process” object. I like to make a parallel and equate the global to the window, and the process to the document. I know they’re different, but this helps me make that transposition.

Another fun thing:

console.log(Object.getOwnPropertyDescriptors(process))

Now we’ll want to create our server.

Don’t panic… I added the directory name and file name just to see something in the browser other than “hello world”

The new constant variable, “server”, is really just chaining. This has the same effect, but condensing things down a bit makes it easier to read. Yay! We’ll be doing some more of this too.

http.createServer is going to utilize an arrow function which takes in a request and a response. With the response, we’re going to set a statusCode equal to 200. We’ll also be setting our headers with .setHeader(name, value). Later, it’ll set to ‘text/html’ but for now, we’re just going to display boring, simple text. Finally, we’ll end our response with response.end(), and pass in a simple message along with our fun directory and file names. Do note: response.end() MUST be called on each response, as this method signals to the server that everything has been sent and the message is now complete.

Before we start this little wonder up, we have to call our server with server.listen().

I enjoy console.log() maybe a bit more than the next dev. It helps me feel not so alone and like the machine is talking to me. Pretty boring conversation, but I’ll take it!

Before we run this, we’ve chained together a bit here! Let’s take a look and remove the variables.

I left the dir and file name variables because they aren’t really part of the chaining. It is sort of interesting to see it like this, if for nothing else, just to see it. Naturally, this is not the recommended way to write it out.

To start the server, simply type in terminal: node server

Presuming everything is correct, you should have a local node server running.

--

--

No responses yet