簡體   English   中英

對於 AWS lambda 無服務器 nodejs 應用程序,您如何在開發環境中執行請求?

[英]for an AWS lambda serverless nodejs app, how do you execute a request in the development environment?

我正在接管一個 AWS lambda 無服務器 nodejs 應用程序,我正在嘗試弄清楚如何在我的開發環境中執行端點。 出於我的目的,我正在嘗試模擬如下請求:

http://localhost:3000/find/product?hiveProductId=22002233&zipCode=44035

該應用程序在 handler.js 中包含以下內容

app.route("/find/product").get(cors(), async (req, res) => {
    try {
        console.log(
            "finding product",
            req.query
        );

        etc...
    } catch (error) {
        console.error("caught error finding product: ", error);
        res.status(500).json(error);
    }
});

module.exports.find = serverless(app);

serverless.yml中還有以下內容:

functions:
  find:
    handler: handler.find
    events:
      - http:
          path: /find/product
          method: GET
          memorySize: 128
          timeout: 30
          private: false
          cors: true
          integration: lambda-proxy
          request:
            parameters:
              querystrings:
                hiveProductId: true 
                max: false
                lat: false
                lon: false
                allowGeoIp: false
                zipCode: false
            methodReponses:
              - statusCode: "200"
                responseBody:
                description: "array of stores with product"
                responseModels:
                    "application/json": "Stores"
              - statusCode: "404"
                description: "not found"
      - http:
          path: /find/stores
          method: GET
          memorySize: 128
          timeout: 30
          integration: lambda-proxy
          private: true
          request:
            parameters:
              querystrings:
                max: false
                lat: true
                lon: true
          documentation:
            summary: "Find the closest stores"
            queryParams:
              - name: "lat"
                description: "latitude caller for geosearch"
                required: true
              - name: "lon"
                description: "longtitude caller for geosearch"
                required: true
              - name: "max"
                description: "maximum stores to location to return. default is 5"
            methodReponses:
              - statusCode: "200"
                responseBody:
                  description: "array of stores sorted by distance"
                responseModels:
                    "application/json": "Stores"

我一直在使用https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke-local/作為參考。 serverless invoke local似乎是我正在尋找的。 serverless invoke local --function find給出以下響應:

{
    "statusCode": 404,
    "headers": {
        "x-powered-by": "Express",
        "content-security-policy": "default-src 'none'",
        "x-content-type-options": "nosniff",
        "content-type": "text/html; charset=utf-8",
        "content-length": "139"
    },
    "isBase64Encoded": false,
    "body": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /</pre>\n</body>\n</html>\n"
}

任何關於如何正確使用serverless invoke的建議或指示,或不同的研究方法,或任何更有成效的文檔,將不勝感激。

我以前沒有用過這種方法,所以我不能說什么。 我可以推薦 serverless-offline 作為不同的解決方案。 無服務器離線

您在問如何測試和調試無服務器應用程序。這是我正在使用的.vscode/launch.json (放置在 VS Code 項目的根目錄中)(我一直在使用 VS 代碼):

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Lambda",
      "runtimeExecutable": "/Users/ninjablade/.nvm/versions/node/v12.18.0/bin/node",
      "cwd": "${workspaceFolder}/lambda-mock",
      "program": "/usr/local/bin/sls",
      "args": [
        "invoke",
        "local",
        "-f",
        "resolvers",
        "-p",
        "../mocks/resolvers.json"
      ],
      "skipFiles": ["<node_internals>/**"]
    },
  ]
}

正如您在此處所注意到的,我們正在運行$ sls invoke local -f function -p data命令(您可以從 sls 框架文檔中查看此命令的確切指南)

作為該命令的-p選項,我們可以傳遞端點所需的任何信息。 您可以輕松地從 Cloud Watch 日志中獲取測試事件數據,並將其替換為您的測試輸入數據。

$ sls invoke local命令將在您的本地開發環境中模擬完全相同的 lambda function 環境。

這篇文章將幫助您通過 VS Code 調試無服務器應用程序 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM