Running a Server Locally with Node and Express

Michael Madden
4 min readDec 28, 2018

--

At first, coding your own server may seem daunting. You may understand the basic principles, like the server listening for calls on a certain port, and then responding based on what request it received, but how is it actually done?

Here, I’ll show you how to start your own server using Node.js, a server side run time environment for JavaScript.

We will set up the server to run on our personal computer. For simplicity’s sake, we’ll only send it requests from our computer. We do this with the localhost:{portnumber} url in our browser (Replace {portnumber} with the port the server is listening on).

Before we get coding, we’re going to need to install a couple of tools to help us. First, if you don’t have a preferred text editor for coding, I suggest Visual Studio Code. Its UI is clean, intuitive, and fast. It has tons of available plugins, and comes with an in editor command line (Press ctrl+` to open the command line when in the editor).

I’ll be using Visual Studio Code as my editor, and Git Bash as my terminal. Both are personal preferences, any editor/terminal combination will work.

The tool we’ll really need is Node.js. This is will enable us to run JavaScript on our computers, and not just a browser.

Once you’ve installed Node.js, open your terminal, type node -v, and hit enter. You should get a response like v8.12.0, this is the version number of node that you installed. This confirms that node has been installed successfully. Next, enter npm -v. Npm is the package manager for JavaScript, which means it handles the installing and updating of relevant software (like the JavaScript libraries). You should get a response like 6.4.1 (also the version number).

Now let’s get all of our files ready.

Create a folder in whatever directory you want this project to be in. The Desktop works just as well as Documents, it doesn’t matter. Copy the address of the folder you’ve created by right clicking on it and selecting “Copy Address” in the file explorer.

In your terminal, type in cd (with a space afterwards) and then right click to paste the copied address. Hit enter, and the cd command will Change Directory to the address of the folder you just made. Stay in this directory.

Enter the command npm init in your terminal, it will walk you through a series of prompts, most of them (except the entry point) you can just hit enter to continue. It will give you the prompt entry point: (index.js), type app.js and press enter. This sets app.js as the first file to be run.

Now type npm install express --save into the terminal. This JavaScript module, Express, will help us get our server up and running. --save saves this new dependency to the package.json file that was created when you ran npm init.

Lastly, create a file called app.js. This is where we will be writing all of our code.

Now, to begin coding.

The first thing we’ll need to do is include our Express library in our code. To do this, write const express = require('express'); at the very top of your file. The value that is stored in express is actually a function, so now we call it and assign it to a variable with const app = express();. “app” is now storing the functionality of the Express framework, and it is what we are going to use to make our server work.

In order for our server to be able to receive requests, it has to be listening for them. With express, we can tell it to listen with the line app.listen(3000). 3000 is the port of our computer that we are telling the server to listen on.

I like to add a callback to this line, to make sure that the server has actually started running and no errors have occurred. We can do this with the below code.

app.listen(3000, function() {
console.log("Server is listening on port 3000...");
});

Now we’re going to write the first GET route. To do this, above the app.listen, write

app.get('/', function(req, res) {
res.send("Hello world!")
});

The ‘/’ indicates that this route will be hit when someone enters the url of our server’s port without anything else at the end of it. This is the root route

Now let’s run the server.

In the terminal, still in the same directory, type node app.js. This will run the app.js file in the Node environment. Your terminal should look like

Now go to your browser and type in http://localhost:3000/. This is the “url” of your server. When the page loads, it should display the “Hello world!” message.

Congratulations! Your server is officially running!

If you want to add another GET request with a different path: use the same format of the earlier one, but just use a different path, like this!

app.get('/dogs', function(req, res) {
res.send("This is the dog page!");
});

Now go to http://localhost:3000/dogs and you should see a page that says: “This is the dog page!”

Right now the server only does GET requests, but you can do POST requests too (to send data to the server), with the help of a tool like Postman.

See the code on Github here

--

--

Michael Madden

Computer Science student at Penn State | Developer for Regavi CMS