无状态响应模板,状态代码为

时间:2016-09-30 13:56:28

标签: serverless-framework

我想使用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"

但它不起作用。如何使用状态代码编写响应模板。?

1 个答案:

答案 0 :(得分:3)

无服务器V1当前未使用状态代码。但是有一个无服务器插件可以使用状态代码

1.install插件

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 = function(event,context){    context.succeed({ “位置”: “https://test.com”}) }

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

    回复:$ {self:custom.redirectResponses}

相关问题