Building a Serverless REST API using MongoDB(mongoose), NodeJs and Serverless framework
Tags: serverless, mongodb, node, api
Published: 2018-05-01 20:32:03

---
title: Building a Serverless REST API using MongoDB(mongoose), NodeJs and Serverless framework
published: true
description: I created this post, because while I was trying to deploy my first Serverless API, I realized the great lack of info, about how to do it using mongoose and how to test it locally.  
tags: Serverless,Mongodb,NodeJs,API 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/wv04xo3pdw9mlhfe11vh.jpg 
---
#Goals
Build and test locally a Serverless API,  that can access a local MongoDB using mongoose
#Requirements 
All right fellows, first I need to explain the reason that I created this post. I'm a young BackEnd developer, and I've encountered a lot of information about the creation of Serverless API's, but I realized a great lack of articles when it comes to build using mongoose and testing locally. Anyway, this is a quite simple tutorial, but if you're just like I was yesterday, maybe it can save you some hours.

This lesson is specific for people who already know almost everything to set up this API, but is suffering because of two or three poorly explained things. So, if you don't know how to set up your AWS account, or don't  know how to use the serverless framework, here are some videos that helped me: 
[How to create and deploy a simple Serverless API](https://www.youtube.com/watch?v=71cd5XerKss&t=322s)
[Understanding a little bit more about AWS LAMBDA](https://www.youtube.com/watch?v=PEatXsXIkLc&index=2&list=PLWYz5kbNpmYVOHUtCwWkRsDtjACI4NfGb)
[Installing MongoDB](https://www.youtube.com/watch?v=WH5GgHaEy7E&t=360s)
#Let's go to work!
###Ok, So let's start installing the Serverless package:
    npm install -g serverless 
###Now let's create a default service using the aws-nodejs template
    sls create -t aws-nodejs -p api && cd api
###Now you can see inside the folder we just created named 'api', that we have 2 files:'handler.js' and 'serverless.yml'
###Now, inside your api folder, let's initialize npm
    npm init
###Now, let's install mongoose
    sudo npm install mongoose --save
###Ok guys, above, in the "Requirements" session, I recommended this  [Video](https://www.youtube.com/watch?v=71cd5XerKss&t=322s), it will explain how the basic function inside our file works, re-watch if you feel needs, because we're gonna apply some changes on those files. 
###First, make sure you run "mongod" command, because we need a local MongoDB service.
###Okay... Now, let's create a new folder inside our api directory, let's name it "Schemas", inside it we're gonna create a new Schema for our MongoDB, if you're not used to work with schemas, check this mongoose [Doc](http://mongoosejs.com/docs/guide.html) about them 
    (inside our directory named 'api') mkdir schemas 
###Then
    (inside schemas) touch user.js
###Using some editor(I like Atom), edit user.js structure inside a var and export a module with this one
![Just like this](https://thepracticaldev.s3.amazonaws.com/i/98lp77l2rtekr7t78cor.png)
###All right, now inside "handler.js" let's modify the same "hello" function to connect with our running Mongod service and execute a simple query, but before we need to import "mongoose", start a connection, get our userSchema and create a model.
    (inside handler.js) var mongoose = require("mongoose");
###Then
    var connectorMongodb =  mongoose.connect('mongodb://localhost/mynewDB');
###Almost there...
    var userSchema = require('../api/schemas/userSchema.js');
###Okay...
    var userModel = mongoose.model('user',userSchema,'user');
###Just to remember  you guys, this lesson isn't for total beginners, so if you don't know how to use mongoose and what is a model or schema, go work on it first. This lesson is specific for people that are getting into trouble because of the lack of information about create a Serverles Rest API using specifically mongoose.
###Ok, now let's apply some changes inside our "handler.js", and remember that we must close our mongoose connection once it was open, otherwise our function will "timeout" (Lambda functions have a default amount of seconds until it stops) 
![Like this](https://thepracticaldev.s3.amazonaws.com/i/u9mxbb8una6f7rkzt536.png)
###Now, inside your api directory, run this command in order to test your function "hello" locally
    sls invoke local --function hello
### It should returns you a Json with your  user's collection information
### I guess you should already have some user data stored inside your DB, just go to mongoshell and use db.user.insert
#I hope i helped you guys, see you later.
[My Instagram if you like to discuss anything with me](https://www.instagram.com/delimanicolas/)