【问题标题】:Gatsby Serverless Function Error: 404 Not FoundGatsby 无服务器函数错误:404 未找到
【发布时间】:2021-06-10 15:46:39
【问题描述】:

我一直在尝试在我的 Gatsby 网站中设置用于发送电子邮件的无服务器功能。该函数正在托管,但每当我尝试获取它时,它都会告诉我它不存在。

我的收获:

const body = {
  name,
  email,
  phone,
  message,
};

fetch(`http://localhost:8000/.netlify/functions/sendMail`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
})
  .then((res) => console.log(res.data))
  .catch((err) => console.error(err));

gatsby-config.js中的中间件:

developMiddleware: (app) => {
  app.use(
    "/.netlify/functions/",
    createProxyMiddleware({
      target: "http://localhost:8000",
      pathRewrite: {
        "/.netlify/functions/": "",
      },
    })
  );
},

函数路径:

    • 功能
      • 发送邮件
        • node_modules
        • package.json
        • sendMail.js
        • yarn.lock

我认为这与我在fetch() 函数中获取的网址有关。

【问题讨论】:

    标签: javascript reactjs gatsby serverless netlify-function


    【解决方案1】:

    如果您在开发中使用netlify-lambda serve <functions folder> 编译和提供函数,则需要将target 设置为netlify-lambda 侦听器绑定的端口,默认为9000。现在,您正在重定向到端口 8000,这似乎是您运行 Gatsby 应用程序的端口。

    因此,例如,您希望您的设置看起来更像这样:

      // Forward Netlify Function requests to `netlify-lambda` server
      // when the development server is running.
      developMiddleware: app => {
        app.use(
          "/.netlify/functions/",
          proxy({
            target: "http://localhost:9000", // <--- Port that `netlify-lambda` is using
            pathRewrite: {
              "/.netlify/functions/": "",
            },
          })
        )
      },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 2020-09-25
      • 2010-12-20
      • 2012-04-06
      相关资源
      最近更新 更多