1

我正在尝试使用本地测试 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 也可以正常工作。问题可能是什么?

4

2 回答 2

0

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

于 2018-12-15T15:05:11.230 回答
0

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

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

于 2018-08-02T17:39:18.683 回答