RESTful API with Node.js, Express, and DynamoDB

Faerul Salamun
3 min readMay 17, 2021

Hello everyone, in this article I want to share how to build Restful API with Node.js, Express, and database DynamoDB.

From the official website, Node.js is an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. We will use the framework Express.js and database DynamoDB from AWS.

Source (https://upload.wikimedia.org/wikipedia/commons/f/fd/DynamoDB.png)

DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It’s a fully managed, multi-region, multi-active, durable database with built-in security, backup and restore, and in-memory caching for internet-scale applications. First, make sure you have an AWS account, and then open your console DynamoDB and create a Table.

In this tutorial, I create table Users and use Primary key UserID with type data String without sort key. In Read/write capacity mode I choose On-demand.

So, let’s start, prepare your IDE. Create a project structure as shown in the image below.

Create a file .env in the root project, change it to your config dynamodb.

Create file database.js in folder src > helpers. In this file, we configure a connection from Node.js to DynamoDB.

Next, we will create a user module. There are 3 parts to the user module :

  • Repository manages database operations.
  • Service manages business logic like manipulation data, validation, etc.
  • Controller manage request/response.

Create file user.repository.js. This file will manage database operations like Find data a User by UserID, Create a User, Update a User by UserID, and Delete a User by UserID. We use library UUID for the Primary key UserID.

Next, create user.service.js. This file will manage return data from user.repository.js

Last, in the user module create file user.controller.js

Next, we will set up a route endpoint for this API. There are 4 endpoints for this API :

1. GET /api/v1/users/:UserID fetch data user by UserID
2. POST /api/v1/users create data user
3. PATCH /api/v1/users/:id update data user by UserID
4. DELETE /api/v1/users/:id delete data user by UserID

The question is, why there is no endpoint to get all users? If you want to create an endpoint to get all users you must create GSI. I will create the tutorial later :).

Next, create a file server.js. We will set up express.js. The application use port 3000. Library body-parser use to getting data from the request body.

Last, create a file app.js. In this file, we will call file server.js and run createServer. Library dotenv use to manage environment dynamodb.

We will test the application use postman. Run command :

node app.js or if you use nodemon, nodemon app.js

Post User /api/v1/users

Body :

{
"Username":"faerulsalamun"
}

GET User By ID /api/v1/users/:UserID

Update User /api/v1/users/:UserID

Body :

{
"Username":"faerulsalamun"
}

Delete User /api/v1/users/:UserID

The source code for this tutorial can be found here.

--

--