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.
- Create a new directory for your code
mkdir simpleapi && cd $_- Create a package based on default settings
npm init --y- Add the express package to the file package.json
npm i express- At this point your package.json should look similar to below:
{
"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"
}
}- Edit the package.json file and update the script section to include a start option
{
...
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 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.
-
Use an editor to create a new file called
index.js -
Add the following content to the index.js file
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'});
})-
Save the file
-
The directory should now contain the following files
.
├── index.js
├── node_modules
├── package.json
└── package-lock.jsonTest the REST API
- Run the application
npm start-
Open a new terminal
-
Test the GET API using the following command:
curl localhost:8080The above command will return a response similar to below:
{
Status: "OK",
Payload: "GET: Up and Running"
}- Test the POST api using the following command:
curl -X POST localhost:8080The above command will return a response similar to below:
{
Status: "OK",
Payload: "POST: Up and Running"
}Congratulations your API is up and running!
Repository
Source Code for this blog post is available here: