Use Express, hapi, or other NodeJS web frameworks on API Gateway and Lambda
Inspired by https://github.com/awslabs/aws-serverless-express
npm install lambda-http
'use strict';
const http = require('http');
const express = require('express');
const lambdaHttp = require('lambda-http');
let app;
let server;
exports.handler = lambdaHttp.newHandler(function(socketPath, callback) {
app = express();
server = http.createServer(app);
app.get('/foo', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ message: 'hello bar'}));
});
server.on('listening', function() {
console.log('server listening on socket: ' + socketPath);
callback(null);
});
server.on('error', callback);
server.listen(socketPath);
});
'use strict';
const Hapi = require('hapi');
const lambdaHttp = require('lambda-http');
let server;
exports.handler = lambdaHttp.newHandler(function(socketPath, callback) {
server = new Hapi.Server();
server.connection({ port: socketPath });
server.route([
{ method: 'GET', path: '/foo', config : { auth: false, handler : function(request, reply){ return reply({ message: 'hello bar'}); } } },
]);
server.start(function(error) {
if (!error) {
console.log('server listening on socket: ' + server.info.uri);
}
callback(error)
});
});