-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (71 loc) · 2.32 KB
/
index.js
File metadata and controls
88 lines (71 loc) · 2.32 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var express = require('express');
var AWS = require('aws-sdk');
var app = express();
// Configuring AWS - Change the aws.json file, here is how to get your localdev credentials: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-your-credentials.html
AWS.config.loadFromPath('./aws.json');
// This is the name of the lambda function you will inject from aws
var injectedLambdaMethod = '<YOUR_LAMBDA_NAME_HERE>';
// Instantiating Lambda
var lambda = new AWS.Lambda({});
// Creating a controller to test our remote method
app.get('/:first/:second', function (req, res) {
var payload = {
'firstNumber': req.params.first,
'secondNumber': req.params.second
};
// Prepare payload to send to Lambda
var pullParams = {
FunctionName : injectedLambdaMethod,
InvocationType : 'RequestResponse',
Payload: JSON.stringify(payload)
};
// Invoke remote lambda function
lambda.invoke(pullParams, function(err, data) {
if (err) {
res.send(err);
} else {
res.send(JSON.parse(data.Payload).body);
}
});
});
// Starting the MS
var server = app.listen(3000, function () {
console.log('Listening...');
});
/*
// ONE PLUS ONE METHOD (to put as a lambda)
exports.handler = (event, context, callback) => {
var first = +event.firstNumber;
var second = +event.secondNumber;
var total = first+second;
// doing a simple x + Y
const response = {
statusCode: 200,
body: first + " + " + second + " is " + total
};
callback(null, response);
};
*/
/*
// ONE MULTIPLIES ONE METHOD (to put as a lambda)
exports.handler = (event, context, callback) => {
var first = +event.firstNumber;
var second = +event.secondNumber;
var total = first*second;
// doing a simple x * Y
const response = {
statusCode: 200,
body: first + " * " + second + " is " + total
};
callback(null, response);
};
*/
/*
// ADD VALIDATION
if (!Number.isInteger(event.firstNumber)) {
callback(null, {body: event.firstNumber + " is not a number"})
}
if (!Number.isInteger(event.secondNumber)) {
callback(null, {body: event.secondNumber + " is not a number"})
}
*/