-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.sw-api-example.js
44 lines (35 loc) · 1.24 KB
/
app.sw-api-example.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
43
44
'use strict';
/**
* @author Jim Lynch
*
* The name of this file and object is exposes is "app". It is common in express.js examples to refer to the webserver
* being created as "app". This way we can handle our endpoints in a very similar way we do with non-servereless
* development.
*/
const express = require('express');
const app = express();
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const StarWarsFunctions = require('./src/star-wars-functions');
app.use(awsServerlessExpressMiddleware.eventContext())
app.starWarsFunctions = new StarWarsFunctions();
app.getHandler = function (req, res) {
app.lambdaParams = {};
if (req.apiGateway) {
app.lambdaParams = Object.assign({}, app.lambdaParams, req.apiGateway.event)
}
if (req.query) {
app.lambdaParams = Object.assign({}, app.lambdaParams, req.query)
}
res.set({
'Content-Type': 'application/json',
'charset': 'utf-8'
});
return app.starWarsFunctions.getCharacterData(app.lambdaParams.character).then(characterData => {
res.send(characterData);
}, err => {
res.send(err);
});
};
app.get('/', app.getHandler);
// Export the Express configuration so that it can be consumed by the Lambda handler.
module.exports = app;