15
loading...
This website collects cookies to deliver better user experience
Serverless computing is a method of providing backend services on an as-used basis. The servers are still used, but the developer(s) or the company that gets backend services from a serverless vendor is charged based on his/her or their computation—usage, not as a fixed amount of bandwidth or number of servers.
curl -o- -L https://slss.io/install | bash
curl -o- -L https://slss.io/install | VERSION=2.21.1 bash
choco install serverless
npm install -g serverless
npm install --global fauna-shell
fauna cloud-login
serverless-crud
project that would easily help us with a demo code template to perform CRUD functions with our application.serverless install --url https://github.com/faunadb/serverless-crud
todos
to debt_tracker
.cd serverless-crud
npm install
serverless.yml
file and then look for the field with the name MY_FAUNADB_SERVER_SECRET
and replace the value with our API key we’ve copied and saved previously. Also, do the same as above for the package.json
file. serverless.yml
file, which simply is a config file that is specific to Serverless framework, contains metadata of the utilities that will be looked into soon. Our focus will be mainly on the functions field and all its values. This contains the necessary information on how our serverless functions are structured to perform:functions:
create:
handler: handler.create
events:
- http:
path: debt_tracker
method: post
cors: true
readAll:
handler: handler.readAll
events:
- http:
path: debt_tracker
method: get
cors: true
readOne:
handler: handler.readOne
events:
- http:
path: debt_tracker/{id}
method: get
cors: true
update:
handler: handler.update
events:
- http:
path: debt_tracker/{id}
method: put
cors: true
delete:
handler: handler.delete
events:
- http:
path: debt_tracker/{id}
method: delete
cors: true
functions
that contain a file: handler.js
. Within this file are defined utilities, functions, that are defined and exported for specific actions. We've five serverless functions from the above code that will perform the CRUD operation to our database that we had set up earlier within this tutorial. So let's navigate into the handler.js
file to explore the functions.handler.js
file is where our serverless functions, defined within the same working directory with the handler.js
are imported to make different specific requests to the database.("use strict");
const debt_trackerCreate = require("./todos-create.js");
const debt_trackerReadAll = require("./todos-read-all.js");
const debt_trackerReadOne = require("./todos-read-one.js");
const debt_trackerUpdate = require("./todos-update.js");
const debt_trackerDelete = require("./todos-delete.js");
module.exports.create = (event, context, callback) => {
debt_trackerCreate(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.readAll = (event, context, callback) => {
debt_trackerReadAll(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.readOne = (event, context, callback) => {
debt_trackerReadOne(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.update = (event, context, callback) => {
debt_trackerUpdate(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
module.exports.delete = (event, context, callback) => {
debt_trackerDelete(event, (error, result) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(result)
};
context.succeed(response);
});
};
curl -X POST https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker --data '{ "debt" : "Owing the UN a visit" }'
curl https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker
curl https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker/<id>
curl -X PUT https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker/<id> --data '{ "debt" : "Owing my travel agency" }'
curl -X DELETE https://XXXX.execute-api.region.amazonaws.com/dev/debt_tracker/<id>
15