chris bailey

Basics of Creating Web APIs with Node.JS and Express

Fast, unopinionated, minimalist web framework for Node.js

Express is a web application framework built on node.js. In this post, I'll explain the basics of setting it up and creating a simple web application.

Setup

Let's get this series of posts started by creating a basic app that will send back a predetermined string to the web browser. First, create a new directory and cd into it.

mkdir hello-world && cd hello-world Once we're in there, we need to create our package.json to store modules needed for our application and store basic information about the project. We'll run the following npm init -y, which will run through the process without asking any extra questions of us, if you'd like the more verbose method, remove -y from the command. We now have a package.json that looks like this:

/* ./package.json */
{
    "name": "hello-world",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
}

Let's run through this line by line really quickly.

  • "name" is the project's name, which is just grabbed directly from the folder name.
  • "version" is what version of application this is currently.
  • "description" is where we put a small application description.
  • "main" is the filename of the main application file. By default, this is set to index.js.
  • "scripts" are where we can add things to run by typing "npm *".
  • "keywords" takes an array of strings and is used to help people find your package when uploaded to npm.
  • "author" is the name of the person creating the project.
  • "license" whatever license you're releasing under. defaults to ISC

Next up, let us install express with npm so we can access it in our application. In the hello-world directory, type the following in terminal npm i express. This will add express to our package.json under a new section called "dependencies". And we now have access to express and can use it in our application. We'll continue by creating our index.js and filling it with a basic application to send a message to the browser when hitting the root path.

// index.js
const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('root path');
});

app.listen(3000, () => {
  console.log('listening on port 3000');
});

The rundown:

  • First, we import the express module and assign it to a const since this won't change.
  • We then create a const app that calls express and gives us access to all the modules we need.
  • app.get(... tells express to watch for a GET request on the given path, which is the first argument it takes '/' in our case. The second argument is the callback function that then tells the server to send the message 'root path' back to user once the path has been hit.
  • We then start up the server on port 3000 and return a console message, so we know it's running.

That's it for this portion of my express series. We've created a basic server that sends a message back when we hit the '/' path on our localhost. Easy Peasy. Next, we'll get into view rendering and some different types of engines we can use.