So I have set up a very simple node.js server that runs a very simple html page. In the page, the user writes a message and I want to take in that message as input on my server side and use one of my api gateway methods i generated to send it back to my dynamoDB table. I have no clue how to do this... or what code to write. The documentation is not very good for this and im really counting on your help!
here is my simpleServer.js
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var hostname = '127.0.0.1';
var port = 3000;
var fs = require('fs');
var app = express();
var AWS = require('aws-sdk');
var apigateway = new AWS.APIGateway({accessKeyId: 'MYKEY', secretAccessKey: 'MYSECRETKEY'});
app.use(bodyParser.urlencoded({
extended:true
}));
app.get('/',function(req,res)
{
console.log(req.url);
fs.readFile('index2.html',"utf-8",function(err,html) {
if(err)
{
throw err;
}
res.send(html);
});
});
app.post('/',function(req,res)
{
var type = req.body.type;
var email = req.body.email;
fs.readFile('index2.html',"utf-8",function(err,html) {
if(err)
{
throw err;
}
res.send(html);
});
});
app.listen(3000,function()
{
console.log("starting server");
});
Here is my index.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>hello there</h1>
<form method="POST" action="/">
<input type="text" name="sampleName">
<input type="submit" name="sampleButton" value="submit">
</form>
</body>
</html>
And here is my api-gateway sdk code that was generated.
/*
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
var apigClientFactory = {};
apigClientFactory.newClient = function (config) {
var apigClient = { };
if(config === undefined) {
config = {
accessKey: '',
secretKey: '',
sessionToken: '',
region: '',
apiKey: undefined,
defaultContentType: 'application/json',
defaultAcceptType: 'application/json'
};
}
if(config.accessKey === undefined) {
config.accessKey = '';
}
if(config.secretKey === undefined) {
config.secretKey = '';
}
if(config.apiKey === undefined) {
config.apiKey = '';
}
if(config.sessionToken === undefined) {
config.sessionToken = '';
}
if(config.region === undefined) {
config.region = 'us-east-1';
}
//If defaultContentType is not defined then default to application/json
if(config.defaultContentType === undefined) {
config.defaultContentType = 'application/json';
}
//If defaultAcceptType is not defined then default to application/json
if(config.defaultAcceptType === undefined) {
config.defaultAcceptType = 'application/json';
}
// extract endpoint and path from url
var invokeUrl = 'https://m5ohekq1p3.execute-api.us-west-2.amazonaws.com/emailAPI';
var endpoint = /(^https?:\/\/[^\/]+)/g.exec(invokeUrl)[1];
var pathComponent = invokeUrl.substring(endpoint.length);
var sigV4ClientConfig = {
accessKey: config.accessKey,
secretKey: config.secretKey,
sessionToken: config.sessionToken,
serviceName: 'execute-api',
region: config.region,
endpoint: endpoint,
defaultContentType: config.defaultContentType,
defaultAcceptType: config.defaultAcceptType
};
var authType = 'NONE';
if (sigV4ClientConfig.accessKey !== undefined && sigV4ClientConfig.accessKey !== '' && sigV4ClientConfig.secretKey !== undefined && sigV4ClientConfig.secretKey !== '') {
authType = 'AWS_IAM';
}
var simpleHttpClientConfig = {
endpoint: endpoint,
defaultContentType: config.defaultContentType,
defaultAcceptType: config.defaultAcceptType
};
var apiGatewayClient = apiGateway.core.apiGatewayClientFactory.newClient(simpleHttpClientConfig, sigV4ClientConfig);
apigClient.addemailPost = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, ['body'], ['body']);
var addemailPostRequest = {
verb: 'post'.toUpperCase(),
path: pathComponent + uritemplate('/addemail').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, []),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(addemailPostRequest, authType, additionalParams, config.apiKey);
};
return apigClient;
};
So not really sure what to fill in, and what code to write to really hook it all up. I did not write any code yet for keys or anything because im not sure what needs to be filled!
Really hoping for your guidance guys.
via TheQ
No comments:
Post a Comment