我使用 vercel 托管了我的作品集网站,当我尝试发送邮件时,我收到此错误
Error: Error: ENOENT: no such file or directory, open 'src/app/emailcount.json'
at async open (node:internal/fs/promises:636:25)
at async Object.readFile (node:internal/fs/promises:1246:14)
at async n6 (/var/task/.next/server/app/api/sendmail/route.js:4:88894)
at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36258
at async eR.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:26874)
at async eR.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:37512)
at async es (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25465)
at async en.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1026)
at async r6.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:508)
at async r6.renderPageComponent (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:5121) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'src/app/emailcount.json'
}
这是我的函数的代码
import EmailTemplate from "@/app/components/EmailTemplate";
import { NextRequest, NextResponse } from "next/server";
import { Resend } from "resend";
import { promises as fs } from "fs";
const resend = new Resend(process.env.RESEND_API_KEY);
export async function POST(req: NextRequest) {
try {
const file = await fs.readFile("src/app/emailcount.json",
"utf8"
);
const emailcount = JSON.parse(file);
const { name, email, phonenumber, emailsub, message }: any = await req.json();
console.log("name is:", name);
const test = "portfolio" + emailcount.count;
console.log(test);
const data = await resend.emails.send({
from: `${name} <${test}@resend.dev>`,
to: "[email protected]",
subject: emailsub,
react: EmailTemplate({
name: name,
phonenumber: phonenumber,
email: email,
emailsub: emailsub,
message: message,
}),
});
console.log("data is:", data.data?.id);
// Update the email count after a successful send
emailcount.count += 1;
await fs.writeFile( "src/app/emailcount.json",
JSON.stringify(emailcount, null, 2),
"utf8"
);
return NextResponse.json({
data,
message: "Email sent successfully",
success: true,
status: 200,
});
} catch (error) {
console.error("Error:", error);
return NextResponse.json({
error,
message: "Failed to send email",
success: false,
status: 500,
});
}
}
这是文件结构
它在本地环境(localhost)中正常工作,但部署后它不起作用
我尝试过以不同的方式改变路径
“../../countemail.json” “src/app/countemail.json”
vercel 不允许写入 json 文件,所以这就是部署后无法工作的原因