I've been following this article to deploy a Node REST API to AWS and I've hit a wall. I've set up my serverless.yml
to configure the AWS REST API. As you can see it accounts for this and a DynamoDB table.
plugins:
- serverless-dynamodb-local
service: pair-api
provider:
name: aws
runtime: nodejs10.x
stage: dev
region: us-east-1
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- { "Fn::GetAtt": ["wineTable", "Arn"] }
environment:
WINE_TABLE: wineTable
resources: # CloudFormation template syntax
Resources:
wineTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: wineTable
AttributeDefinitions:
- AttributeName: name
AttributeType: S
KeySchema:
- AttributeName: name
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
functions:
app:
handler: index.handler
events:
- http: ANY /pair/wine
- http: "ANY {proxy+}"
My index.js
file (below) is supposed to populate this endpoint with an array of JS objects. For some reason, when I set message
to test
(see below), I get the error message "Internal Service Error".
const serverless = require("serverless-http");
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
import * as test from "./db/test";
app.use(bodyParser.json({ strict: false }));
app.get("/pair/wine", function(req, res) {
res.send({
success: "true",
message: test
});
});
module.exports.handler = serverless(app);
When I set message to a JavaScript object, (see below), my AWS API end point returns the correct message.
const serverless = require("serverless-http");
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
import * as test from "./db/test";
app.use(bodyParser.json({ strict: false }));
app.get("/pair/wine", function(req, res) {
res.send({
success: "true",
message: [{
"test": true,
"testing": "Test"
}]
});
});
module.exports.handler = serverless(app);
What is the correct way of setting the GET
request for an AWS API so it returns an array of JS objects? I've tried importing JS files a few different ways (see below), but nothing seems to work;
import * as test from "./db/test";
import test from "./db/test";
const test = require("./db/test");
Turns out I needed to read up on importing modules in Node, which leverages ES5 require statements instead of import.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加