我在我的 Astro 项目中运行了 Resend API,在其中我使用私有域向我的电子邮件发送不同的表单。一切都在 json.ts 文件中设置,我在其中执行 POST 方法来发送电子邮件。
在 localhost 中工作正常,但是当我在 Netlify 中部署我的网站时,出现 500 错误。
API KEY 是通过 import.meta.env.API_RESEND 方法从 .env 获取的,我怀疑当我的项目部署在 Netlify 中时这种情况是否会发生变化。
这是我的 json.ts 文件的代码:
import type { APIRoute } from 'astro'
import { Resend } from 'resend'
const resend = new Resend(import.meta.env.API_RESEND)
export const POST: APIRoute = async ({ request }) => {
const body = await request.json()
const { to, from, html, subject } = body
if (!to || !from || !html || !subject) {
return new Response(null, {
status: 404,
statusText: 'Did not provided the right data'
})
}
try {
const send = await resend.emails.send({
from,
to,
subject,
html
})
return new Response(
JSON.stringify({
message: send.data
}),
{
status: 200,
statusText: 'Email sent successfully'
}
)
} catch (err) {
console.log(err.message)
return new Response(
JSON.stringify({
message: err.message
}),
{
status: 500,
statusText: 'Internal server error'
}
)
}
我已经修好了,
我的目标是“/api/sendEmail.json.ts”,而正确的方法是“/api/sendEmail.json”
谢谢!