Skip to content

Latest commit

 

History

History

examples

Examples

Two Modes | Google Cloud Functions | AWS Lambda | Back to Home

User service – in two modes

A simple user service that shows how to build the same application using either function mode or request/response mode. This example includes middleware to require authentication on certain requests, using JWT tokens. It also uses an Express server to mimic a serverless execution environment locally without having to install an emulator. Try it out:

git clone https://github.com/modofunjs/modofun.git

cd modofun/examples/user-function-mode
npm install
npm start

curl -d '{"credentials": "X"}' -H "Content-Type: application/json" -X POST http://localhost:3000/authenticate

Use modofun/examples/user-reqres-mode instead for the request/response version.

 

To-Do service – on Google Cloud Functions

An example To-Do service that includes scripts to deploy it to Google Cloud Functions, or try it out on the local emulator. Install it like this:

git clone https://github.com/modofunjs/modofun.git

cd modofun/examples/todo-google-cloud-functions
npm install

To try it out on Google Cloud, first follow Google's instructions to enable the Cloud Functions API, and install the SDK and components.

Create a stage bucket on Cloud Storage. For the script to work without changes, use the bucket name: modofun-example-src.

gsutil mb -p [project-id] gs://modofun-example-src

Afterwards, when you're ready to deploy the function, use the deploy script:

npm run deploy

curl https://us-central1-[project-id].cloudfunctions.net/myModofunExample/addTodo/joe?todo=Do+the+dishes
curl https://us-central1-[project-id].cloudfunctions.net/myModofunExample/getTodos/joe

Or try it out using the local emulator for Google Cloud Functions:

npm run start-emulator
npm run deploy-to-emulator

curl http://localhost:8010/[project-id]/us-central1/myModofunExample/addTodo/joe?todo=Do+the+dishes
curl http://localhost:8010/[project-id]/us-central1/myModofunExample/getTodos/joe

npm run stop-emulator

 

To-Do service – on AWS Lambda

An example To-Do service that includes scripts to deploy it to AWS Lambda using CloudFormation, or run it locally with the AWS Lambda emulator. Install it like this:

git clone https://github.com/modofunjs/modofun.git

cd modofun/examples/todo-aws-lambda
npm install

To try it out on AWS, first follow AWS's instructions to set up your account and install the AWS CLI (Command Line Interface).

Use the following script to create an S3 bucket to host the code:

npm run create-s3-bucket

When you're ready to deploy the function, use the deploy script, which will package it and deploy it using CloudFormation:

npm run deploy

After the deployment is complete, the script will look up the base URL for the newly deployed API from the AWS CloudFormation Stack. You should see it printed in the console logs when the deployment scripts are finished. It should look like this:

https://[api-id].execute-api.[region].amazonaws.com/Prod/

Then try out the API:

curl https://[api-id].execute-api.[region].amazonaws.com/Prod/addTodo/joe?todo=Do+the+dishes
curl https://[api-id].execute-api.[region].amazonaws.com/Prod/getTodos/joe

You can also try it out using the local emulator for AWS Lambda:

npm run start-local

curl http://127.0.0.1:3000/addTodo/joe?todo=Do+the+dishes
curl http://127.0.0.1:3000/getTodos/joe

But note that there's currently a bug in the AWS local emulator that prevents greedy path variables from working properly.

 

Real-world application

There’s also a real-world example that includes:

All using modofun, combined with Gulp, Babel and other cool technology.