How to Build Your First Node Server

by Nathan Thomas · 15 min read

Image

“Mom, Get the Camera!”

Coding your first server from scratch can seem intimidating when you’ve just started programming, but I promise that you’ll have one up and running before the hour is over if you follow this guide. Don’t worry if you’ve never tried Node or Express, and don’t worry if you’ve never used any of the myriad of software packages engineers use that can seem overwhelming at first. We’re not going to worry about those today.

There are only two assumptions that this article makes — One is that you’re familiar with IDEs (Integrated Development Environments, like Microsoft’s VSCode)/Text Editors, and the other is that you know how to open up a terminal bash window and navigate through folders on your computer with it. For a point of reference, the terminal is the thing that looks sort of like this (although yours may be slightly different):

Image

If both of those statements resonated with you, then let’s move forward with taking the first step in this journey towards our own personal botnet.

Step 1: Install Node.js

Go ahead and open up your terminal window. We need to install node before we can actually create anything. I’m going to assume that you don’t have it installed.

For MacOS:

  1. Type the command /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" into your terminal and hit enter. This will install Homebrew, the package manager that we’ll need to install Node.js. You can check to make sure that Homebrew installed correctly by typing brew -v into the terminal and hitting enter. If it spits you back a line of numbers, that’s the version that you have installed now.
  2. Once that’s done installing, type the command brew install node into the terminal and hit enter. Go grab a coffee while you’re waiting for it to finish.

For Windows:

  1. Go to the Node.js website here and download the appropriate installer bundle for your operating system. If your computer is anywhere remotely close to new, you’ll probably want to use the 64-bit source code download.
  2. Open the installer and follow the directions to install Node.js on your computer.

For Linux:

  1. Linux has the easiest way to install Node, as all you need to is type sudo apt-get install nodejs into your bash terminal and hit enter. This will install Node.js globally for use later on.

Step 2: Time to Find the Ghost in the Machine

Now is where the real fun begins. We’re going to install a few quick dependencies and get your shiny new server up off the ground.

First, go ahead and create a new folder on your computer. I’d recommend naming it something fun. I’m going to call mine dude, my own personal version of the classic “Hello World” application.

Next, go ahead and open that folder in your IDE or text editor of choice. I’m going to be using VSCode, but you are perfectly welcome to use whatever you are comfortable with.

Go ahead and cd in your terminal into the directory that you made for your new server; when you’re there, type the command npm init into your terminal and hit enter. Your computer is going to get cranky and ask you a bunch of irrelevant questions for a project of this size, so go ahead and just hit enter through all of them.

You’re now going to have a setup that looks like this (again, I’m using VSCode):

Image

That little file that was created is your package.json, and it’s what tells Node about your project.

The next thing we need to do is install a single dependency called express. It’s the server framework that will allow us to “flip the switch” on this bad boy. Type npm install express into your terminal window and hit enter; you’re now going to notice that you have “express” listed on the groups of dependencies in your package.json file like this:

Image

The next step that we need to do is create an index.js file that will run our awesome server when we turn it on. Go ahead and create one inside this same folder alongside your package.json and put the following text inside of it:

Image

Please make sure that you create an index.js file and copy the code displayed above exactly as its displayed.

Once you’re done, save the index.js file and go back to your package.json. We need to add one more thing before we’re done, and that’s the start command so that our server will actually know how to turn on. Look for a section that says scripts in your package.json and then modify its contents to look exactly like this:

Image

Good job. Now it’s time for the payoff for our hard work!

Step 3: Flipping the Switch

Go back to your terminal and type in the command npm start. You should see a readout on your terminal that looks something pretty much like this:

Image

See that text reading out that we typed earlier in our index.js document? That means that our server is alive!

Now comes the fun part — Go to your browser and type the following text into your URL bar:

http://localhost:7000/

You should have something that looks like this when you hit enter on your URL bar:

Image

Your very own server is running on your computer and presenting data in your browser from the API route that we wrote! That’s totally cool.

Where you take this in the future is up to you. I’ll have a couple links right below this for you in case you want to continue this journey. Thanks for coding along, and congrats on making your very first server!

Express — Documentation for the Node.js web framework

“Build RESTful APIs with Node and Express” by Programming with Mosh

Thanks for reading.

Nathan

Baby Yoda