-
Notifications
You must be signed in to change notification settings - Fork 0
/
restApiGw.js
42 lines (33 loc) · 1.09 KB
/
restApiGw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// License: Creative Commons Attribution-NonCommercial 4.0 International
'use strict';
// Based on https://steveholgado.com/aws-lambda-local-development/#creating-our-lambda-function
const express = require('express');
const bodyParser = require('body-parser');
function restApiGw(app, restApi) {
app.use((req, res, next) => {
if (req.originalUrl === '/api/webhook') {
bodyParser.raw({ type: 'application/json' })(req, res, next);
} else {
bodyParser.json()(req, res, next);
}
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.text());
app.use(require('cors')());
const apiGw = new (require('./ApiGw'))(restApi);
app.use('/api/*', async (req, res) => {
let route = req.baseUrl.replace(/^\/[^/]*\//, "/");
// invoke should consider reg.method
const result = await apiGw.invoke(
route,
req.method,
{headers: req.headers, body: req.body} //event
);
// Respond to HTTP request
res
.status(result.statusCode ? result.statusCode : 500)
.set(result?.headers)
.end(result.body ? result.body : result.message);
});
}
module.exports = restApiGw;