BACK TO THE HOMEPAGE

Jun 12, 2021 3 min read

Node Rest API

A REST API is super useful and can present a simple way to efficiently access information. This blog post is dedicated to the folks who want to create an API. It seems like a lot of folks are in this boat by the amount of comments over on sites like Reddit. Lets get started by building a GET/POST API in Node.js.

Requirements:

  • Node.js installation
  • Linux based environment
  • Internet access
  • GitHub (Optional) - code is stored there if you get stuck

Overview

In this blog post learn how to make a local REST API that response to both GET and POST requests. The following content can also be applied to a serverless environment. If you need to learn how to apply this to Google Cloud, I highly recommend reading Chapter 8 of my book Hands-On Serverless with Google Cloud.

Node.js application template

The template application will be based on Node.js. If you are unfamiliar with Node.js, this is a standard step to create the files required.

  1. Create a new directory for your code
NODE_TYPE // bash
mkdir simpleapi && cd $_
  1. Create a package based on default settings
NODE_TYPE // bash
npm init --y
  1. Add the express package to the file package.json
NODE_TYPE // bash
npm i express
  1. At this point your package.json should look similar to below:
NODE_TYPE // JavaScript
{
  "name": "simpleapi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}
  1. Edit the package.json file and update the script section to include a start option
NODE_TYPE // JavaScript
{
  ...
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  ...
}
  1. Save the file

Build a REST API

At a high level a REST API is a stateless interface. It relies on HTTP operations (e.g. GET/POST) to perform tasks. In our example Node.js and Express will handle all the complicated stuff.

  1. Use an editor to create a new file called index.js

  2. Add the following content to the index.js file

NODE_TYPE // JavaScript
const express = require('express');
const PORT = process.env.PORT || 8080;
const app = express();

app.use(express.json());

app.listen(PORT, () => {
  console.log(`Simple REST API listening on port ${PORT}`);
});

app.get('/', async (req, res) => {
  return res.status(200).json({'Status':'OK', 'Payload':'GET: Up and Running'});
})

app.post('/', async (req, res) => {
  return res.status(200).json({'Status':'OK', 'Payload':'POST: Up and Running'});
})
  1. Save the file

  2. The directory should now contain the following files

NODE_TYPE // text
.
├── index.js
├── node_modules
├── package.json
└── package-lock.json

Test the REST API

  1. Run the application
NODE_TYPE // bash
npm start
  1. Open a new terminal

  2. Test the GET API using the following command:

NODE_TYPE // bash
curl localhost:8080

The above command will return a response similar to below:

NODE_TYPE // JSON
{
Status: "OK",
Payload: "GET: Up and Running"
}
  1. Test the POST api using the following command:
NODE_TYPE // bash
curl -X POST localhost:8080

The above command will return a response similar to below:

NODE_TYPE // JSON
{
Status: "OK",
Payload: "POST: Up and Running"
}

Congratulations your API is up and running!

Repository

Source Code for this blog post is available here:

rest2serverless