Gatsby Serverless Function Error: 404 Not Found



我一直在尝试在我的盖茨比网站上设置一个发送电子邮件的无服务器功能。函数正在被托管,但是每当我试图获取它时,它都告诉我它不存在。

我的获取:

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()函数中获得它的url有关。

如果您在开发中使用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/": "",
},
})
)
},

最新更新