Express Basics — Part 1

In this tutorial, we’re going to build a very simple Express app in NodeJS. You will learn what you need to get started and how to serve some basic text.

Express is a web application framework designed for NodeJS apps. It is used for everything from serving websites to RESTful APIs and back-end services.

Dependencies: Today we’ll only be using express. In a future tutorial, we will include some more packages to make our app more like a website.
You’ll need NodeJS installed with NPM and a code editor. Find links to NodeJS and a recommended code editor below:

I also recommend installing Yarn. Its a package manager for NodeJS and an alternative to NPM. We’ll be using it in this tutorial.

First we’ll need to include express. Open your code editor, create a new document and add the following to the top of your document:

const express = require('express');

const will declare a variable and also prevent the variable from being re-declared. This will stop us using the express variable later in the app by accident.

The other half, require(‘express’); imports the express library and gives us access to some functions that we will use later on.

Next, we want to declare our app. We will do this by using a function made available to us by express. Add the following to your document beneath the previous code:

const app = express();

express() is a top level function that gives us access to all of the other functions we will be using to add routing to our app. As with the previous code, we do not want our app variable being overwritten elsewhere in the code, so we declare it with const.

Next we’re going to add a route. This is a function provided by express that allows us to send data to the client. In our case, it will take a HTTP request and return some text.

There are many options, we’re going to focus on the get function for this tutorial. Let’s look at the anatomy of a route.

app.get('/', callback);

Let’s explain each part:
app — This is our variable we declared earlier.

.get — This is one of the many functions provided by express. Common functions include get, put, post and delete. These examples are all HTTP Methods. A get request is the type of request your browser makes when you browse to a URL.

‘/’ — The relative path of the request. Setting this to ‘/’ will return data when a client browses to the root path of the server (eg: http://example.com/). Setting this to ‘/home’ will have a similar effect, but when a client browses to the ‘/home’ path (eg: http://example.com/home).

callback — This is a function returned by express, this will contain the variables request and response (we will shorten to req and res respectively).

Let’s look more closely at the callback function. As mentioned earlier, this will return req and res objects. 
req will contain all of the information about the request, this could be the request body, files, cookies and information about the client. We won’t be covering req in this tutorial.
res is the response we send back to the client. We can send various types of data, but for this tutorial we will focus on plain text.

So how do we use res? It’s actually quite simple.

res.send('Hello World!');

Fairly straightforward. The send() function will send data back to the client. In this case, some text. Although you can return all sorts of data: text, strings, integers, arrays and objects.

Let’s put what we’ve learned together, add the following to your document beneath the previous code:

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

Simple right? Now for the final piece.

We need to tell the server to listen for connections and what port to use. As mentioned, express returns a lot of functions. One of those functions is listen(). Add the following to your document, beneath the previous code:

app.listen(3000, function () {
console.log('App listening.')
});

We’re telling our app to listen on port 3000, then we log to the console to let us know that it is working.

Your document should look something like this…

const express = require('express');
const app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('App listening.')
});

Save your document as app.js, and open a terminal in the directory you saved your document. I recommend creating a new directory for your project.

Before we can run the app, we have to install our dependencies. To do this, we’ll be using Yarn. Run the following command in your terminal:

yarn add express

This will install express for us. We’re ready to run our project, to do this, run the following command next:

node app.js

If you’ve followed everything correctly, you should see a message saying ‘App Listening.’
Open your web browser and browse to http://localhost:3000 and you should be greeted with a page that says “Hello World!”.