Middy.js
A powerful Node.js middleware framework for your lambdas
Luciano Mammino (@loige)
29/01/2020, Dublin
Fullstack Dev & Cloud Architect
💬 Ask questions here: loige.link/middy-gcs-qa
Open source
Enterprise
📄
code
⚡️
event
💰
profit
=
* real life coding with Lambda is a little bit more complicated™️ than this...
exports.myLambda = async function (
event,
context
) {
// get input from event and context
// return output or throw an error
}
exports.myLambda = async function (event, context) {
const name = event.queryStringParameters.name
const message = `Hello ${name || 'World'}!`
return ({
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message })
})
}
exports.MyLambda = async function (event, context) {
// decrypt environment variables with KMS
// deserialize the content of the event
// validate input, authentication, authorization
// REAL BUSINESS LOGIC
// (process input, generate output)
// validate output
// serialize response
// handle errors
}
LOTS of BOILERPLATE 😓
npm install @middy/core
Note: using version 1.0.0 (beta)
Give it moar love 😍
const middy = require('@middy/core')
const { middleware1, middleware2, middleware3 } = require('some-middlewares')
const originalHandler = async (event, context) => {
/* your business logic */
}
const handler = middy(originalHandler)
handler
.use(middleware1())
.use(middleware2())
.use(middleware3())
module.exports = { handler }
1. define handler
2. "middify" the handler
3. attach middlewares
4. export "middyfied" handler
const middy = require('@middy/core')
const { middleware1, middleware2, middleware3 } = require('some-middlewares')
const originalHandler = async (event, context) => {
/* your business logic */
}
const handler = middy(originalHandler)
handler
.use(middleware1())
.use(middleware2())
.use(middleware3())
module.exports = { handler }
Business logic
Boilerplate
Some examples:
const middy = require('@middy/core')
const urlEncodedBodyParser = require('@middy/http-urlencode-body-parser')
const validator = require('@middy/validator')
const httpErrorHandler = require('@middy/http-error-handler')
const processPaymentHandler = async (event, context) => {
const {
creditCardNumber, expiryMonth, expiryYear, cvc, nameOnCard, amount
} = event.body
// do stuff with this data ...
return ({
statusCode: 200,
body: 'payment processed correctly'
})
}
const inputSchema = {
// define validation schema here ...
}
const handler = middy(processPaymentHandler)
.use(urlEncodedBodyParser())
.use(validator(inputSchema))
.use(httpErrorHandler())
module.exports = { handler }
Handler
(business logic)
Attach middlewares
Export enhanced handler
Dependencies
Define the input schema
(uses JSON-schema)
const myMiddleware = (config) => {
// might set default options in config
return ({
before: (handler, next) => {
// might read options from `config`
},
after: (handler, next) => {
// might read options from `config`
},
onError: (handler, next) => {
// might read options from `config`
}
})
}
module.exports = myMiddleware
const middy = require('@middy/core')
const handler = middy((event, context, callback) => {
// do stuff
})
handler.before((handler, next) => {
// do something in the before phase
next()
})
handler.after((handler, next) => {
// do something in the after phase
next()
})
handler.onError((handler, next) => {
// do something in the on error phase
next()
})
module.exports = { handler }
Serverless is cool, it helps you to build apps quickly and with a greater focus on business logic, rather than on infrastructure!
Middy helps you to keep focusing on your business logic first
You can add extra behaviours with very minimal changes to your core logic by introducing dedicated middlewares
You can easily share common functionality through middlewares
A special thank you to all the amazing Middy users and contributors!