简体   繁体   English

如何摆脱包装无服务器离线 html 响应正文的双引号?

[英]How to get rid of double quotes wrapping serverless-offline html response body?

I'm trying to get a HTML response from my serverless-offline project, but it returns html body wrapped with double quotes, which ruins the page.我试图从我的无服务器离线项目中获得 HTML 响应,但它返回 html 正文用双引号括起来,这会破坏页面。 How can I get rid of the quotes?我怎样才能摆脱引号? 在此处输入图像描述

Content-type looks valid内容类型看起来有效

在此处输入图像描述

I have this versions of serverless plugins installed我安装了这个版本的无服务器插件

"serverless-base-path-plugin": "0.0.1",
    "serverless-cors-plugin": "^0.2.1",
    "serverless": "1.71.3",
    "serverless-offline": "6.1.7",
    "serverless-optimizer-plugin": "^1.0.0",
    "serverless-resources-validation-plugin": "^0.2.0",
    "serverless-helpers-js": "0.2.0",
    "serverless-webpack": "5.3.2",

serverless.yml无服务器.yml

provider:
  name: aws
  runtime: nodejs8.10
  region: eu-west-1
  profile: default
  memorySize: 256 # optional, in MB, default is 1024
  stage: local

plugins:
  - serverless-offline


custom:
  serverless-offline:
    host: 0.0.0.0
    httpPort: 1465
    useDocker: false
    noPrependStageInUrl: true

functions:
  xdomainProxy:
    handler: dist/xdomain-proxy/handler.handler
    events:
      - http:
         path: /api/xdomain-proxy
         method: get
         integration: lambda
         response:
            headers:
                Content-Type: "'text/html'"

handler.js handler.js

'use strict';

var ServerlessHelpers = require('serverless-helpers-js').loadEnv(); // all content must be assembled into a single variable


function render() {
  return ['<!DOCTYPE HTML>', '<link rel="shortcut icon" href="about:blank">', '<script src="https://cdn.rawgit.com/jpillora/xdomain/0.7.4/dist/xdomain.min.js"></script>'].join(' ');
} // Lambda Handler


module.exports.handler = function (event, context) {
  try {
      var html = render();
      context.succeed(html);
  } catch (err) {
    console.log(err);
    context.fail(err);
  }
};

Ok, looks like it's serverless-offline version 6 issue.好的,看起来它是无服务器离线版本 6 问题。 I could find out that it runs json.stringify if you try to return a string.如果您尝试返回字符串,我会发现它运行 json.stringify。 Downgrading to version 5 solved the issue for me.降级到版本 5 为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法获取路径:/{proxy+} 工作(无服务器离线) - Cant get path: /{proxy+} to work (serverless-offline) 如何在 function 之前在 mocha 测试中启动 serverless-offline - How to start serverless-offline in mocha test before function 无服务器离线 - API REST 节点 typescript - Serverless-offline - API REST node typescript serverless-offline 可选路径参数 - serverless-offline optional path parameter 带有无服务器离线阶段中断路由的 aws-serverless-express - aws-serverless-express with serverless-offline stage breaks routing 如何在无服务器离线中使用 Cognito 事件/触发器调用 lambda 以进行本地测试 - How to invoke lambda with Cognito event/trigger in serverless-offline for local test 在本地 docker localhost:8000 中使用 dynamodb 和在 localhost:4500 上运行的 serverless-framework serverless-offline 应用程序 - Using dynamodb in local docker localhost:8000 with serverless-framework serverless-offline application running on localhost:4500 Serverless-offline 抛出“配置错误”或“无法读取未定义的属性‘选项’” - Serverless-offline throws "Configuration error" or "Cannot read property 'options' of undefined" 如何使用无服务器功能获取 RAW Body? - How to get the RAW Body using serverless functions? 如何将响应正文显示为html - How to display response body into html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM