How do I deploy REST API using an imported array of JS objects?

user10750437

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");
user10750437

Turns out I needed to read up on importing modules in Node, which leverages ES5 require statements instead of import.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I display array objects using a simple loop and 'if' statements?

分類Dev

Im creating a rest api using node.js and in .get method i want to get back a json array

分類Dev

How do i push objects into the same array?

分類Dev

POST array of objects to REST API

分類Dev

How do I save objects that I push into an array, then display them using local storage?

分類Dev

How do I post a pull request comment using BitBucket REST api 2.0?

分類Dev

How do I serve my index.html file while using PHP Slim REST Api?

分類Dev

How do i filter an array inside of a array of objects?

分類Dev

How do I specify the include directory for an imported shared library using CMake?

分類Dev

How do I change the font color using SCSS when it has an imported stylesheet in Angular 8?

分類Dev

How do I create an boolean equals method compares objects in an array

分類Dev

How do I convert array of objects to object in javascript?

分類Dev

How do I create a react list component from an array of objects?

分類Dev

How do I format an array of objects by iteration in Model in Ruby on Rails

分類Dev

how do i sort an array of objects via the comparable interface?

分類Dev

Vue.js: How can I update an array of objects?

分類Dev

How do I delete a collection in Django Rest Api?

分類Dev

How to get the sum of number from an array of objects, using pure JS?

分類Dev

How can I use Canvas Data REST API using python?

分類Dev

How to deploy a ConfigMap using kubernetes client Api

分類Dev

How do I run an imported Spring project in IntelliJ

分類Dev

How do I run an imported Spring project in IntelliJ

分類Dev

How do I map over a contacts array to produce a new array with objects and properties?

分類Dev

Adding JS objects to an array using a cursor loop

分類Dev

How do I create JSON array using QT

分類Dev

How do I pass array information using a factory in Javascript?

分類Dev

How do I select friend ids using the facebook api?

分類Dev

Syncing objects imported at runtime using PUN

分類Dev

How do I loop over an array of objects to return a property dependent on a corresponding property within that object?

Related 関連記事

  1. 1

    How do I display array objects using a simple loop and 'if' statements?

  2. 2

    Im creating a rest api using node.js and in .get method i want to get back a json array

  3. 3

    How do i push objects into the same array?

  4. 4

    POST array of objects to REST API

  5. 5

    How do I save objects that I push into an array, then display them using local storage?

  6. 6

    How do I post a pull request comment using BitBucket REST api 2.0?

  7. 7

    How do I serve my index.html file while using PHP Slim REST Api?

  8. 8

    How do i filter an array inside of a array of objects?

  9. 9

    How do I specify the include directory for an imported shared library using CMake?

  10. 10

    How do I change the font color using SCSS when it has an imported stylesheet in Angular 8?

  11. 11

    How do I create an boolean equals method compares objects in an array

  12. 12

    How do I convert array of objects to object in javascript?

  13. 13

    How do I create a react list component from an array of objects?

  14. 14

    How do I format an array of objects by iteration in Model in Ruby on Rails

  15. 15

    how do i sort an array of objects via the comparable interface?

  16. 16

    Vue.js: How can I update an array of objects?

  17. 17

    How do I delete a collection in Django Rest Api?

  18. 18

    How to get the sum of number from an array of objects, using pure JS?

  19. 19

    How can I use Canvas Data REST API using python?

  20. 20

    How to deploy a ConfigMap using kubernetes client Api

  21. 21

    How do I run an imported Spring project in IntelliJ

  22. 22

    How do I run an imported Spring project in IntelliJ

  23. 23

    How do I map over a contacts array to produce a new array with objects and properties?

  24. 24

    Adding JS objects to an array using a cursor loop

  25. 25

    How do I create JSON array using QT

  26. 26

    How do I pass array information using a factory in Javascript?

  27. 27

    How do I select friend ids using the facebook api?

  28. 28

    Syncing objects imported at runtime using PUN

  29. 29

    How do I loop over an array of objects to return a property dependent on a corresponding property within that object?

ホットタグ

アーカイブ