How to build an API endpoint with Node.js, AWS Lambda and AWS RDS PostgreSQL DB [Part 2]
Intro
The new hype around serverless computing (SC) aka Function-as-a-Service (FaaS) made me curious. So I gave it a try to check it out. Therefore I implemented a basic CRUD application for item entities.
In the first part of this three-part tutorial series, we initialized the application and created our item models with sequelize.js. Now it is time to actually build our API endpoints for the item model and set up the PostgreSQL DB for local development and testing. Thus it is assumed that you got a basic understanding of the command line, REST web services, Node.js, express.js, and PostgreSQL.
In the last part of the series, we will finally deploy our Node.js API application as an AWS lambda function and spin up an AWS RDS PostgreSQL DB.
The API Endpoint
We are now creating the API endpoints for the item entity. Therefore, we have to define the controller itemController
and the routing. The itemController
will handle the creation, updating, deletion and retrieving of the item entities in the PostgreSQL DB. Create a new folder controllers
under …/server/controllers and add a file item.js
. Insert the following code into that file:
To make the controller easily accessible within our app, we export it. Go and create a new file index.js
in the …/server/controllers folder. Add this code:
Now we need to route the incoming API request to the newly created controller function. Hence create the index.js
file under …/server/routes. Insert this code:
The new controller route has to be integrated into the entry file app.js
of our API application. Therefore, import the routes by modifying the app.js
file:
Now our API has the endpoints POST /api/item (this will create an item entity) and GET /api (this will message Welcome to the API!) and GET * (this will message Welcome!). Before actually using the API, we have to set up and connect our PostgreSQL DB.
The PostgreSQL DB
At first, we are going to set up the PostgreSQL locally to be able to test if everything is running. It is assumed that you already have installed PostgreSQL and the DB is running on http://127.0.0.1:5432. Furthermore, I assume that your PostgreSQL has the superuser postgres
with an empty default password (null). You also have to set up a database. We will give it the name db-dev
( createdb db-dev -U postgres
).
So let’s connect our application with the database by properly setting the config.js
file in the …/server/config folder:
We are only focusing on the development stage for the whole tutorial. Here we set the username to postgres, let password empty (null) and the database name to db-dev (as you already specified in the PostgreSQL DB). If the DB is running on the specified host URL, we can use the sequelize command line interface to set up the tables in the DB. Let’s persist the item model to the database with sequelize db:migrate
.
Testing the API endpoints
For testing our API endpoints we can use the postman tool. This lets you specify the POST request with a JSON body. We are trying to save e.g. the item entity: {"item_id": 1234, "name": "laptop", "weight": 1.2}
. You can now go and connect with your local PostgreSQL DB and view the new item entry. Use e.g. pgadmin or the command line tool.
Define the remaining API endpoints
So far we have only built the POST API endpoint for saving an item entity in the DB. But with that foundation, we can easily complete the CRUD application.
Let’s go and add a GET endpoint to list all items and a single item by its ID. Therefore, go back to the …/server/controllers/item.js file and add the list
function underneath the create
function. We will also provide a function to retrieve
a single item via GET /api/item/:itemId (e.g. /api/item/1). Notice, that this ID is not our own specified item_id. But you could also use our ID as well, or any other attribute for retrieval (use the query function findAll()
with where constraint).
Next, add a PUT endpoint to update a single item (/api/item/:itemId). Hence add the update
function to the other functions in the …/server/controllers/item.js file. Again, be aware of the item ID (!= item_id)!
Lastly, we build the DELETE endpoint (/api/item) by adding also the delete
function. Again, be aware of the item ID (!= item_id)!
In the following code snippet, you can see the updated item.js file of our controller:
Before we are finished, we also need to update our routes for the new endpoints. So update the code in the …/server/routes/index.js file:
Go and test your new API endpoints… 🙌
Finally, our Node.js API application runs in our local environment. In the last part, you will see how to easily deploy this app on Amazon’s AWS cloud infrastructure.