Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
cavis committed Apr 22, 2019
1 parent b2c85ab commit ecbbf86
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions dev-server.js
@@ -0,0 +1,41 @@
const express = require('express')
const handler = require('./index')
const app = express()
const port = process.env.PORT || 3000

// pretend to be an api-gateway
app.use(async (req, res) => {
const event = {
resource: (req.path === '/') ? '/' : '/{proxy+}',
path: req.path,
httpMethod: req.method,
headers: req.headers || {},
queryStringParameters: req.query || null,
pathParameters: (req.path === '/') ? null : {proxy: req.path},
stageVariables: null,
requestContext: {},
isBase64Encoded: false,
}
try {
const data = await handler.handler(event)
if (data.statusCode) {
res.status(data.statusCode)
if (data.body && data.isBase64Encoded) {
const buff = Buffer.from(data.body, 'base64')
res.send(buff)
} else if (data.body) {
res.send(data.body)
} else {
res.end()
}
} else {
res.status(500).send(`Lambda returned no statusCode: ${JSON.stringify(data)}`)
}
} catch (err) {
res.status(500).send(`Lambda Error: ${err}`)
}
})

// listener
app.listen(port)
console.log(`Express listening on port ${port}...`)

0 comments on commit ecbbf86

Please sign in to comment.