【问题标题】:Serverless Response Template with Status code带有状态码的无服务器响应模板
【发布时间】:2023-03-04 09:47:02
【问题描述】:

我想为使用 302 http 状态的重定向网页创建响应模板。 我可以使用 aws lambda 用户界面手动执行此操作,但我需要使用无服务器框架版本 v1 执行此操作。

我尝试了以下代码。

response:
        headers:
          Content-Type: "'text/html'"
        template: $input.path('$')
        statusCodes:
             201:
                pattern: '' # Default response method
             302:
                pattern: 'http.*'*'
                template:  '$input.path('$')'
                headers:
                     Content-Type: "integration.response.body.errorMessage"

但它不起作用。如何编写带有状态码的响应模板?

【问题讨论】:

    标签: serverless-framework


    【解决方案1】:

    无服务器 V1 目前无法使用状态码。但是有一个无服务器插件用于使用状态码

    1.安装插件

    npm i serverless-plugin-multiple-responses --save
    

    2。在 serverless.yml 文件中添加插件名称

    plugins:
      - serverless-plugin-multiple-responses
    

    3。在 serverless.yml 文件中添加此自定义代码(您可以根据需要更改此代码)

    custom:
       defaultRegion: us-east-1
       region: ${opt:region, self:custom.defaultRegion}
       stage: ${opt:stage, env:USER}
       standardRequest:
          template:
             application/json: ${file(./templates.yml):request}
       standardResponseHeaders:
          'Access-Control-Allow-Origin': "'*'"
          'Content-Type': 'integration.response.body.headers.Content-Type'
          'Expires': 'integration.response.body.headers.Expires'
          'Cache-Control': 'integration.response.body.headers.Cache-Control'
          'Pragma': "'no-cache'"
       standardResponseTemplate: "$input.path('$.body')"
       errorResponseHeaders:
          'Access-Control-Allow-Origin': "'*'"
          'Expires': "'Thu, 19 Nov 1981 08:52:00 GMT'"
          'Cache-Control': "'no-cache, max-age=0, must-revalidate'"
          'Pragma': "'no-cache'"
       errorResponseTemplate: "$input.path('$.errorMessage')"
       # Here we are defining what would be under "responses" in your HTTP event
       # if you were not using the custom variables.
       standardResponses:
          200:
             headers: ${self:custom.standardResponseHeaders}
             templates:
                'application/json;charset=UTF-8': ${self:custom.standardResponseTemplate}
          404:
             headers: ${self:custom.errorResponseHeaders}
             templates:
                'application/json;charset=UTF-8': ${self:custom.errorResponseTemplate}
             properties:
                SelectionPattern: '.*\"status\":404.*'
          500:
             headers: ${self:custom.errorResponseHeaders}
             templates:
                'application/json;charset=UTF-8': ${self:custom.errorResponseTemplate}
             properties:
                SelectionPattern: '.*\"status\":500.*'
       redirectResponses:
          # Since we want to return 302 upon a successful completion, we remove the
          # built-in default of 200
          200: false
          302:
             headers:
                Location: "integration.response.body.headers.Location"
             templates:
                'application/json;charset=UTF-8': "$input.path('$.body')"
                'text/html;charset=UTF-8': "$input.path('$.body')"
          404:
             headers: ${self:custom.errorResponseHeaders}
             templates:
                'application/json;charset=UTF-8': "$input.path('$.body')"
                'text/html;charset=UTF-8': "$input.path('$.body')"
             properties:
                SelectionPattern: '.*\"status\":404.*'
          500:
             headers: ${self:custom.errorResponseHeaders}
             templates:
                'application/json;charset=UTF-8': "$input.path('$.body')"
                'text/html;charset=UTF-8': "$input.path('$.body')"
             properties:
                SelectionPattern: '.*\"status\":500.*'
    
    1. 在您的处理程序文件中添加重定向网址

      module.exports.home = 函数(事件,上下文){ context.succeed({"位置":"https://test.com"}) }

    2. 在 serverless.yml 文件中定义函数及其响应

      响应:${self:custom.redirectResponses}

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2016-09-11
      • 1970-01-01
      相关资源
      最近更新 更多