Starting Your First Node/Express Javascript Server

This guide is old and has been refactored. View the new guide here.

If you prefer to follow video tutorials, I have recorded a quick run through of this tutorial for your viewing.

I have decided to attempt to publish my process from start to finish on developing a Software as a Service (SaaS) company due to some requests I have had. I will be starting with development, and I don't really have a strict timeline on anything. Posts could end up being infrequent.

I will be writing my application primarily in JavaScript. I will be using Node.js, MongoDB, Express, and React.js. I have little to no experience in any of these things, so I will be learning as I go. If you know more than I do about something, or if I do something wrong, please point it out in the comments so we can all learn together and fix things.

In order to follow along, I think you will need a base understanding of HTML, CSS, databases, and JavaScript. Pain tolerance would also be a good thing to have, because this can get really frustrating, really quickly.

In this first post, I want to run through the simplest of steps, which are getting the basic dependencies installed and starting a server with Express. I also want to take a second to give an overview of what the different pieces of this stack are.

Node.js and Express will handle the server-side of our application. This is where we will build our API, handle authentication, and create a great deal of our app's logic. MongoDB is the NoSQL database we will be using. It stores data in a JSON (JavaScript Object Notation) format, which will be easily accessible and modifiable to our app. Finally, React.js (developed by Facebook, and it actually powers Instagram). React is our view handler. I don't want to get any more in-depth on that at this point. Feel free to read more about it, or you will find out soon enough when we cover it.

I will include links to the installation help pages for each of the dependencies for people using other operating systems, but I will be working on and providing instructions for Mac.

I will try to keep this Github repo up to date with this guide as well. Click here to see the repository.

Let's get started...

First, download and install Node.js. They have an installer that will pretty much do it for you, but you can also use Homebrew by entering the following in your terminal:

brew install node  

Because Express and React can simply be installed as Node packages, we will install MongoDB next. Go download and install MongoDB via the install package or by using Homebrew again. Enter the following in your terminal:

brew install mongodb  

As per the instructions MongoDB itself provides, you must create the folder /data/db. Since we don't technically need to use our database right now, you can leave this alone for the time being. If you want to play with your new toy a bit, try entering the following into your terminal:


This will start up your database locally.

Next, let's install Express and React, so we can start up our server and get things moving along. This part is easy. Now, let's make a new project folder somewhere and then navigate to it. In your terminal:

mkdir saas  
cd saas  

In order to start getting our packages from Node Package Manager (NPM), we will want to create a package.json file. In the terminal:

touch package.json  

Open this file in your favorite code editor. Atom is a great and free code editor. A lot of people prefer Sublime Text. The choice is yours! Place the following in your package.json file:

  "name": "saas",
  "main": "server.js"

Now, we can finally install our packages! In the terminal:

npm install --save express mongoose  

Great! Now we have our initial dependencies. We will add more later, but this is enough to start our server and then some. Also, check your package.json file again! You'll notice it looks a little more like this (version numbers may vary):

  "name": "saas",
  "main": "server.js",
  "dependencies": {
    "express": "^4.13.4",
    "mongoose": "^4.4.1",

This is because NPM added the dependencies for you (thank the --save bit for that). Excellent! It's about time to get the server started! Create a new file, server.js by entering the following in your terminal:

touch server.js  

Now open the file you just created and enter and save the following:

// Include our packages in our main server file
var express = require('express');  
app = express();  
var mongoose = require('mongoose');  
var port = 3000;

console.log('Your server is running on port ' + port + '.');  

Now you are ready to start your server! Type the following in your terminal:

npm start  

You will see your server starting up. Now, in your console, you should see "Your server is running on port 3000."

That's it for this section! Please check out part two, where we create an authentication system for our Node API using Passport and JSON Web Tokens.

Joshua Slate

Entrepreneur. Rock climber. Software engineer. Founder of @SlatePeak and others.

Saint Paul, MN

Subscribe to SlatePeak

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!