This examples demonstrates how to use Moesif AWS Lambda middleware for Node.js with Middy and Serverless Framework.
This example builds and deploys a simple HTTP API to AWS using Serverless right from your terminal. The API has a single valid endpoint greet/
that expects POST
requests. A valid POST
request body looks like the following:
{
"greetingsMessage": "Good evening!",
"greeterName": "Alex"
}
The example uses the following Middy middlewares:
.
├── handler.js
├── package.json
├── README.md
└── serverless.yml
The AWS Lambda function and the API logic lives inside handler.js
. The serverless.yml
file describes your Serverless app's infrastructure. For more information, see Serverless.yml Reference.
Before running this example, make sure you have performed the following steps:
- Have an active Moesif account
- Have your Moesif Application ID
- Set up Serverless with with your AWS account
This example uses ECMAScript modules to comply with the latest 5.x version of Middy.
If you want to run this example using CommonJS, use Middy version 4.x. Additionally, you must make the following adjustments:
- Remove
"type": "module"
from thepackage.json
file. - Change the
import
andexport
statements to CommonJS-style imports and exports.
-
Clone this repository and enter its directory:
cd moesif-middy-serverless-example
-
Specify your Moesif Application ID in the
.env
file:MOESIF_APPLICATION_ID=YOUR_MOESIF_APPLICATION_ID
-
Install the dependencies:
npm install
-
Then deploy with Serverless:
serverless deploy
For a successful deployment, the
deploy
command generates an output similar to the following:Deploying "moesif-middy-serverless" to stage "dev" (us-east-1) ✔ Service deployed to stack moesif-middy-serverless-dev (69s) endpoint: POST - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/greet functions: greetingsHandler: moesif-middy-serverless-dev-greetingsHandler (3.7 MB)
-
Send requests to the API endpoint. In your Moesif Web Portal, you should see API traffic details show up.
If you face any issues running this example, reach out to our support team or open an issue.