2016-09-30 2 views
2

Я хочу создать шаблон ответа для переадресации веб-страницы с использованием 302 http status. Я могу сделать это вручную с помощью пользовательского интерфейса aws лямбда, но мне нужно сделать это без серверной версии 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" 

Но он не работает. Как написать шаблон ответа со статусом.

ответ

3

Сервер без сервера V1 в настоящее время не работает со статусом. Но есть бессерверной плагин для кода состояния использования

1.Install плагин

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

2. Добавить плагин имя в файле serverless.yml

plugins: 
    - serverless-plugin-multiple-responses 

3. Добавить пользовательский код в бессерверную .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. Добавить URL перенаправления в файл обработчика

    module.exports.home = функция (событие, контекст) { context.succeed ({ "Местоположение": "https://test.com"})}

  2. Определение функции и ее ответы в serverless.yml файле

    ответы: $ {самоуправления: custom.redirectResponses}

 Смежные вопросы

  • Нет связанных вопросов^_^