Page Content Section
NodeJS REST API using MongoDB, AWS Lambda, and Cognito
Hydrating the FrontEnd Using a Serverless Stack!
Read more about developing serverless APIs for use in web applications, using Node JS, NoSQL, and other AWS Services.
The Details
One of the greatest benefits of using cloud infrastructure is that it offers several options for providing data to applications. For instance, with Amazon Web Services it's possible to utilize Amazon Elastic Compute Cloud (EC2) for setting up a host of services or REST APIs. However, in some cases, simply being able to provide data through an event-driven function-based microservice could suffice for many reasons.
This is where Node JS becomes quite handy because it's a powerful JavaScript runtime environment that can be used to develop REST APIs through a variety of techniques.
The Data Flow
With the case of eflintdev.com (this site), I've provided it data using a Node API that is provisioned through serverless microservices. The serverless API runs on Node JS v. 18+ and the data is sourced from a MongoDB database. Below we have a diagram which illustrates all the major components of this setup:
AWS Cognito Is In There
You probably noticed the inclusion of AWS Cognito in the previous data flow diagram, and this is especially vital for protecting the routes that either write, update, or delete. Such is achieved using JWT based tokens provided through a confidential client auth flow for authentication. Also, though it's integral to the permissions scheme, AWS IAM (Identity Access Management) was excluded from the diagram for the sake of simplicity. But IAM permeates the cloud infrastructure and works to secure or provide access control for the components in this setup.
AWS API Gateway REST API - Good-to-go!
It's possible to provide access to an externally or preconfigured API Gateway REST API using the provider.apiGateway data in the serverless.yml file, as I've done.
Environment Variables from .env
Serverless YAML Configuration
AWS Gateway in AWS Management Console
Once deployed the stages and methods fall into place and can be tweaked inside of the AWS Management Console if desired. Otherwise, a developer can continue to stack on the settings in the serverless.yml.
Ensuring Data Structure(s) with Mongoose
The data for this project has a specific data structure that is modeled using Mongoose Schema. This made it possible to also configure some type validation measures, so that data is not added to the MongoDB database without meeting some specific requirements.
Mongoose is also featured with the means to allow developers to create custom validators. This utility and middleware affords a few more helpful utilities for customizing how data is accessed, or delivered from a database/ collection:
Indexes
- Indexes
- Counters
- Custom Field Selection
- Custom Field Exclusion
GET, POST, PATCH, and DELETE
The four necessary requests were developed using Mongoose for CRUD operations. I've provided some code snippets for the GET and POST requests.
GET
POST
Unit Tests Examples
Unit testing was implemented for each route using JEST. It has some great support for testing Promises, which is the primary coding pattern for all the http requests in this API.
GET
Deployment
It was easy to deploy this serverless project using the Linux terminal. Moreover, it's also convenient to test the serverless functions using the command line as well. However, as mentioned previously, Jenkins can be utilized with Ansible for deploying serverless applications, as well. Ideally, this should be done using an Ansible role, a working Jenkins setup, and Ansible Agent installed in Jenkins.