Sitemap

Serverless Todo API Tutorial

3 min readFeb 4, 2023

--

Introduction:

A couple of months ago I created a MERN todo application for a tutorial. Lately I’ve been learning and using the serverless framework so I wanted to recreate the backend for that tutorial using serverless rather than expressjs. This tutorial will cover those changes. Once you finish this you can follow along with my MERN Todo Tutorial — Part Two to build the front end.

Project Setup:

First you’ll need to create a root directory for your api. Once that directory is created you can run ‘npm init’ within the directory and then install all the required npm packages: ‘npm install –save-dev serverless serverless-bundle serverless-offline’ and ‘npm install mongoose’. These are all the packages we will need to run the project locally and use mongoose for mongodb connection.

From here we will need to create the serverless.yml file and update our package.json to use serverless to run the api locally:

Update your package.json to the code below:

Press enter or click to view image in full size

Add a serverless.yml file to the root of your directory and put the following code in it. The plugins section tells our serverless project which plugins to utilize. The provider illustrates which platform we will be using, in this case I was using AWS Lambda. The functions section I will explain in more detail as we move along:

Functions:

The primary code in a serverless project are the functions that will be implemented. In AWS case these will be Lambdas which connect to an AWS API Gateway to handle the http integration. I place these functions in the root of my project but if the project gets too large you can separate them out for more organization:

Get Web Dev Junkie’s stories in your inbox

Join Medium for free to get updates from this writer.

Get Todos:

Press enter or click to view image in full size

Create Todo:

Press enter or click to view image in full size

Update Todo:

Press enter or click to view image in full size

Delete Todo:

Press enter or click to view image in full size

For the Todo model I’m using the same code from my previous MERN project. I’ve also created a ResponseWrapper class for handling the return values from the lambda. This is also in the root directory of the serverless project:

Press enter or click to view image in full size

In order to wrap everything up we have to finish the serverless.yml file. As you’ll see in the code we will be outlining all of our functions in the file so that serverless knows how to handle our api endpoints:

Conclusion:

With all of these pieces in place we’ve successfully built a simple todo API with all of the CRUD functions we need to make it work with our Todo ReactJS front end. You can find the repository here the serverless api code is in the ‘serverless-api’ directory. The front end react code can be found in the ‘todo-client’ directory. Serverless also has some fantastic documentation on how to use their framework here

--

--

Web Dev Junkie
Web Dev Junkie

Written by Web Dev Junkie

Web developer with an interest in Javascript, AWS and all things Web Development