This tutorial will show you how to build a simple REST API with Node.js and Express.js, a popular and lightweight framework for building Node.js apps.

Getting Started

You need to have both Node and NPM already installed to complete this tutorial. Here is a tutorial for installing Node/NPM on a Mac and on a Windows computer.

On the command line, create a new folder, navigate to it, and then run npm init:

mkdir test
cd test
npm init

npm will ask a number of questions which you can hit Return to leave as the default for now.

Installing NPM

Once complete, npm will create a package.json file that tracks project information and dependencies. To confirm you have this file, type ls:

Next install Express by running this command:

npm install --save express

Your First REST API

We can create a REST API with one file and six lines of code. Ready? Create a new file named index.js with the following code:

const express = require('express');
const app = express();

app.get('/api/todos', (req, res)=>{
	res.json({todo: 'My todo app. Edit info here!'})
});

app.listen(3000);
console.log('Listening on localhost:3000');

What we’ve done here is:

  • imported the Express module
  • initialized the Express object
  • created a handler for GET /api which responds to requests with a JSON object
  • told Express to listen to HTTP requests on port 3000
  • added a console.log statement to confirm the server is running

Now run this on the command line:

node index.js

Installing NPM

If you open your web browser to localhost:3000/api you’ll see our single REST API endpoint running.

Installing NPM

Add Multiple Routes

Within our api/todos endpoint let’s add multiple routes corresponding to multiple todos.

The new code looks as follows:

const express = require('express')
const app = express()

app.get('/api/todos', (req, res) => {
  let todos = [
    {
      id: 1,
      todo: "My rest API."
    },
    {
      id: 2,
      todo: "Add an endpoint."
    },
    {
      id: 3,
      todo: "Write a tutorial."
    }
  ];
  res.json(todos)
});

app.listen(3000)
console.log('Listening on localhost:3000')

On the command line, press CTRL + the “C” key to stop the server. Then type node index.js again to restart the server. If you again navigate to localhost:3000/api you’ll see multiple routes now.

Installing NPM

Multiple Endpoints and Routes

For our final step, let’s add an additional endpoint for users at /api/users and populate it with routes.

const express = require('express');
const app = express();

app.get('/api/todos', (req, res) => {
  let todos = [
    {
      id: 1,
      todo: "My rest API."
    },
    {
      id: 2,
      todo: "Add an endpoint."
    },
    {
      id: 3,
      todo: "Write a tutorial."
    }
  ];
  res.json(todos)
});

app.get('/api/users', (req,res)=>{
  let users = [
    {
      id: 1,
      name: "Ryan Dahl",
    },
    {
      id: 2,
      name: "TJ Holowaychuk"
    },
    {
      id: 3,
      todo: "Brendan Eich"
    }
  ];
  res.json(users)
});

app.listen(3000)
console.log('Listening on localhost:3000')

If we navigate to localhost:3000/api we’ll see it’s the same but we can also go to localhost:3000/api and see our new “users” endpoint.

Installing NPM