【问题标题】:ECONNRESET when testing netlify functions and Gatsbyjs 2测试 netlify 函数和 Gatsbyjs 2 时的 ECONNRESET
【发布时间】:2018-07-31 05:16:11
【问题描述】:

我正在尝试使用 netlify-lambda 在本地测试 Netlify 的 lambda 函数,但无法让代理与 Gatsbyjs 一起使用。我一直在控制台中收到这个:

[HPM] Rewriting path from "/.netlify/functions/add-subscriber" to "/add-subscriber"
[HPM] GET /.netlify/functions/add-subscriber ~> http://localhost:9000
[HPM] Error occurred while trying to proxy request /add-subscriber from localhost:8000 to http://localhost:9000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

这是我在请求中得到的

尝试代理时出错:localhost:8000/add-subscriber

这是我在gatsby-config.js 文件中的内容

developMiddleware: app => {
  app.use(
    "/.netlify/functions",
    proxy({
      target: "http://localhost:9000",
      pathRewrite: {
        "^/\\.netlify/functions": ""
      },
      logLevel: "debug"
    })
  )
}

这就是我提出请求的地方

onSubmit(values) {
  fetch('/.netlify/functions/add-subscriber')
    .then(resp => resp.json())
    .then(resp => console.log(resp))
    .catch(e => {
      console.error(e)
    })
}

我尝试了几个示例,但没有一个对我有用。我还关闭了所有可能使用这些端口的程序。从浏览器测试 URL (http://localhost:9000/add-subscriber) 工作正常。直接获取 URL 也可以正常工作。可能是什么问题?

【问题讨论】:

    标签: node.js webpack gatsby netlify http-proxy-middleware


    【解决方案1】:

    我注意到的第一件事是您的pathRewrite 与文档中的示例不匹配,您使用的是"^/\\.netlify/functions",而文档使用的是"/.netlify/functions/"。修复就这么简单吗?

    如果您通过curl 或直接在浏览器中加载http://localhost:8000/.netlify/functions/add-subscriber,会发生什么情况?这应该会告诉您配置是否正常工作。

    【讨论】:

      【解决方案2】:

      更改为 target: 'http://[::1]:9000', 对我有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-05
        • 2021-01-15
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多