Check out bidbear.io Amazon Advertising for Humans. Now publicly available 🚀

Express Basics

Intro

Express.js is a minimalist web application framework for Node.js. It is a bare bones framework that handles a lot of the basic functions of a web application, such as connecting to a database and handling http requests.

This post will cover the basics of Express.js, such as installation and setup on a Node.js server.

Here is a bare bones Express application file. We will break down everything in this file in this post.

// ***************************
// SETUP
// ***************************

console.log("app.js is connected");

// import express module
var express = require("express");
// set express to variable
var app = express();

// set listen port
app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Listening on PORT: " + process.env.PORT + " at IP: " + process.env.IP);
});

// ***************************
// ROUTES
// ***************************
app.get("/", function(req, res){
    res.send("Welcome to the Home Page");
});

File Structure

A basic file structure for an Express App would be the following.

app.js file

In the root of your node.js server create a containing folder for the application and within that folder create a Javascript file titled app.js (the name “app” is arbitrary but this will be the master index for the application).

Installing Express

Express.js is a package that you will install with NPM. Instructions for installing the package are available at expressjs.com, but at the time of this writing it is:

$ npm install express --save

Import Express Module

Next we must import the module into our application. Add the following to our app.js file:

var express = require("express");

The name of the variable is arbitrary. The name of the required package is not.

Set Express function() to Variable

Now that we have imported the module, we must create a variable that executes Express, and we will then be able to call upon all of the Express methods on that variable. A common variable name for this is “app”, though you can choose anything.

var express = require("express");
var app = express();

Methods

Listen For Requests

Express must be told to listen for requests, by telling it what port to listen on.

.listen()

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

In this example we are listening on port 3000, which is standard for a local installation.

If you are using Cloud9 you will set your port to

app.listen(process.env.PORT, process.env.IP);

This is an environment variable that will return the PORT of the environment that we are working on, as well as the HOST IP.

set listen port

And then we can set a little callback function that logs in the console:

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Listening on PORT: " + process.env.PORT + " at IP: " + process.env.IP);
});

Which will then tell us our PORT and IP in the console

listening on port 8080 at IP 127.0.0.1

Routes

Routes are bits of code, that will run other bits of code, depending on the request that is received on the server. For example, if the server receives a GET request for “/” (the index) send them to the homepage. If server receives GET request for “/contact” send them to the contact page, Etc.

Remember that the order of routes matters. The application checks them sequentially. If you place a wildcard route at the start of the list no other response will ever be served.

.get()

When a user makes a GET request, we want this code to run. So the first argument that this method takes is the GET location, and the second is the callback function.

app.get("/", function(req, res){
// the function code
});

req and res are short for request and response.

req and res are objects inside of this function. So obviously we will be referencing them in the function.

app.get("/", function(req, res){
    res.send("Welcome to the Home Page");
});

This will send our simple little text message as the response.

.get(“*”)

The * “star” or “splat” is the wildcard url. So any requested URL that does not have a specific response listed will get the response listed for this route. For example

// wildcard
app.get("*", function(req, res){
    res.send("I'm sorry, we couldn't find that page");
});

This is how you direct to a 404 page.

The wildcard route must be placed at the end of your routes list, because the order or routes matters! Your program is going to go down the list of routes sequentially until it finds a match. If you place the wildcard at the top of the sequence it will match and direct all get requests to this page.

.post()

Post is for sending data to be added to the server. There is a detailed walk-through of the .post method here: Express Writing and Testing Routes

.delete()

.patch()

.put()

Starting the Server

You will only be able to start the server after you have done the following four things

  • Install Express
  • Import the Express module
  • Set express to variable
  • Set the Listen Port

After you have done that you start the server with:

node app.js

And if you have succeeded you will no longer be able to type commands, and you will see the little rectangle letting you know you are in the Express console.

node app is running

to exit the Express console type ctrl + C

Note that the server must be restarted for any changes in the application to take affect.

Amazon Ad Analytics For Humans

Advertising reports automatically saved and displayed beautifully for powerful insights.

bidbear.io
portfolios page sunburst chart