node.j mysql rest_使用MongoDB(mongoose),NodeJ和无服务器框架构建无服务器REST API

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

Understanding a little bit more about AWS LAMBDA

Installing MongoDB

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, 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 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

e2c3fabef65e19e6118b9e19570b07bc.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)

82e7b14bd31b8dcb9d370742f5139e20.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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值