Node Rest API

Share on:

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
1mkdir simpleapi && cd $_
  1. Create a package based on default settings
1npm init --y
  1. Add the express package to the file package.json
1npm i express
  1. At this point your package.json should look similar to below:
 1{
 2  "name": "simpleapi",
 3  "version": "1.0.0",
 4  "description": "",
 5  "main": "index.js",
 6  "scripts": {
 7    "test": "echo \"Error: no test specified\" && exit 1"
 8  },
 9  "keywords": [],
10  "author": "",
11  "license": "ISC",
12  "dependencies": {
13    "express": "^4.17.1"
14  }
15}
  1. Edit the package.json file and update the script section to include a start option
1{
2  ...
3  "scripts": {
4    "start": "node index.js",
5    "test": "echo \"Error: no test specified\" && exit 1"
6  },
7  ...
8}
  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

 1const express = require('express');
 2const PORT = process.env.PORT || 8080;
 3const app = express();
 4
 5app.use(express.json());
 6
 7app.listen(PORT, () => {
 8  console.log(`Simple REST API listening on port ${PORT}`);
 9});
10
11app.get('/', async (req, res) => {
12  return res.status(200).json({'Status':'OK', 'Payload':'GET: Up and Running'});
13})
14
15app.post('/', async (req, res) => {
16  return res.status(200).json({'Status':'OK', 'Payload':'POST: Up and Running'});
17})
  1. Save the file

  2. The directory should now contain the following files

1.
2├── index.js
3├── node_modules
4├── package.json
5└── package-lock.json

Test the REST API

  1. Run the application
1npm start
  1. Open a new terminal

  2. Test the GET API using the following command:

1curl localhost:8080

The above command will return a response similar to below:

1{
2Status: "OK",
3Payload: "GET: Up and Running"
4}
  1. Test the POST api using the following command:
1curl -X POST localhost:8080

The above command will return a response similar to below:

1{
2Status: "OK",
3Payload: "POST: Up and Running"
4}

Congratulations your API is up and running!

Repository

Source Code for this blog post is available here:

rest2serverless