├── .gitignore ├── .npmignore ├── History.md ├── Makefile ├── Readme.md ├── index.js ├── package.json └── test └── serve.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.1 / 2016-11-10 3 | ================== 4 | 5 | * bump aws-serverless-express to fix header formatting issues 6 | * simpler example 7 | 8 | 1.0.0 / 2010-01-03 9 | ================== 10 | 11 | * Initial release 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter spec 6 | 7 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # lambda-serve 3 | 4 | Use koa or express on lambda! This is just a simple wrapper around [aws-serverless-express](https://github.com/awslabs/aws-serverless-express) 5 | 6 | ## Installation 7 | 8 | ```js 9 | npm install lambda-serve 10 | ``` 11 | 12 | ## Koa example 13 | 14 | ```js 15 | const Serve = require('lambda-serve') 16 | const koa = require('koa') 17 | const app = koa() 18 | 19 | /** 20 | * Add proxy support 21 | */ 22 | 23 | app.proxy = true 24 | 25 | /** 26 | * Run 27 | */ 28 | 29 | exports.default = Serve(app.callback()) 30 | 31 | /** 32 | * Respond to requests 33 | */ 34 | 35 | app.use(function * () { 36 | this.body = this.ip 37 | }) 38 | ``` 39 | 40 | ## Setup 41 | 42 | Follow https://github.com/awslabs/aws-serverless-express#steps-for-running-the-example to get started. 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | /** 4 | * Module Dependencies 5 | */ 6 | 7 | const serverless = require('aws-serverless-express') 8 | 9 | /** 10 | * Export `Server` 11 | */ 12 | 13 | module.exports = Server 14 | 15 | /** 16 | * Create a server 17 | */ 18 | 19 | function Server (listener) { 20 | const server = serverless.createServer(listener) 21 | return function (event, ctx) { 22 | serverless.proxy(server, event, ctx) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-serve", 3 | "version": "1.0.1", 4 | "description": "Use koa or express on lambda!", 5 | "keywords": [ 6 | "serverless", 7 | "express", 8 | "koa", 9 | "proxy", 10 | "lambda" 11 | ], 12 | "author": "Matthew Mueller ", 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/MatthewMueller/lambda-serve.git" 16 | }, 17 | "dependencies": { 18 | "aws-serverless-express": "^1.2.0" 19 | }, 20 | "main": "index", 21 | "devDependencies": { 22 | "mocha": "3.1.2", 23 | "roo": "2.0.1" 24 | } 25 | } -------------------------------------------------------------------------------- /test/serve.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module Dependencies 3 | */ 4 | 5 | const Server = require('../index') 6 | const assert = require('assert') 7 | const roo = require('roo') 8 | 9 | /** 10 | * Tests 11 | */ 12 | 13 | describe('lambda-serve', () => { 14 | 15 | it('should work with weird bodies', (done) => { 16 | const api = roo() 17 | api.post('/', function * () { 18 | this.body = this.request.body 19 | }) 20 | 21 | const event = { 22 | 'path': '/', 23 | 'httpMethod': 'POST', 24 | 'body': '{"article": "“lea"}', 25 | 'headers': { 26 | 'Content-Type': 'application/json' 27 | } 28 | } 29 | const ctx = { 30 | succeed: (res) => { 31 | assert.ok(res.headers.date) 32 | delete res.headers.date 33 | assert.deepEqual(res, { 34 | statusCode: 200, 35 | body: '{"article":"“lea"}', 36 | headers: { 37 | 'content-type': 'application/json; charset=utf-8', 38 | 'content-length': '20', 39 | connection: 'close' 40 | } 41 | }) 42 | 43 | done() 44 | } 45 | } 46 | 47 | const server = Server(api.listener()) 48 | server(event, ctx) 49 | }) 50 | 51 | }) 52 | --------------------------------------------------------------------------------