错误:函数中定义的模式“api/email.js”与“api”目录中的任何无服务器函数不匹配(Vercel/Remix-run)

问题描述 投票:0回答:1

email.js 由 chatgpt 在我的 root/api/email.js 目录中生成的无服务器函数:

//[root]/api/email.js
//just a test function generated by chatgpt
import sendEmail from "../utils/email";

export const config = {
  runtime: "edge",
};

export default async function handler(req) {
  try {
    const { email, subject, html } = await req.json();
    const response = await sendEmail(email, subject, html);

    if (response.error) {
      return new Response(JSON.stringify(response.error), { status: 500 });
    }

    return new Response(JSON.stringify(response.data), { status: 200 });
  } catch (error) {
    console.error(error);
    return new Response("Internal Server Error", { status: 500 });
  }
}

vercel.json 在我的根目录中:

//[root]/vercel.json
{
  "version": 2,
  "functions": {
    "api/email.js": {
      "maxDuration": 60
    }
  }
}

上下文:我有一个 remix.run 应用程序,其中我的重新发送/nodemailer 电子邮件功能在发送电子邮件之前超时。我在网上读到,您可以使用 vercel 无服务器函数来解决此问题,方法是将函数/文件配置为在 60 秒(而不是默认的 10 秒)后超时。

问题: 为什么我收到错误:“错误:函数中定义的模式

api/email.js
api
目录中的任何无服务器函数不匹配”
当我运行
vercel dev

我试过:

  1. 将 vercel.json 中的
    api/email.js
    更改为
    email.js
  2. 将api目录放到app目录下

我假设解决方案是使用 vercel.json 配置文件以某种方式将 vercel 定向到正确的 api 文件夹?

File structure

reactjs serverless vercel remix.run
1个回答
0
投票

我发现你可以使用 maxDuration 属性向混音路由添加一个配置变量:

删除 vercel.json 文件:

//[root]/vercel.json
{
  "version": 2,
  "functions": {
    "api/email.js": {
      "maxDuration": 60
    }
  }
}

删除 api 文件夹和 email.js 无服务器功能:

//[root]/api/email.js
//just a test function generated by chatgpt
import sendEmail from "../utils/email";

export const config = {
  runtime: "edge",
};

export default async function handler(req) {
  try {
    const { email, subject, html } = await req.json();
    const response = await sendEmail(email, subject, html);

    if (response.error) {
      return new Response(JSON.stringify(response.error), { status: 500 });
    }

    return new Response(JSON.stringify(response.data), { status: 200 });
  } catch (error) {
    console.error(error);
    return new Response("Internal Server Error", { status: 500 });
  }
}

向路由添加配置变量:

//app/routes/register.jsx
import Auth from "../components/Auth";
import { authenticator } from "../../services/auth.server";
import { redirect } from "@remix-run/node";
import { register } from "../../services/authService.server";
import { useActionData } from "@remix-run/react";

export const meta = () => {
  return [
    {
      title: "",
    },
  ];
};

export const config = {
  maxDuration: 60,
};

export const loader = async ({ request }) => {
  const user_id = await authenticator.isAuthenticated(request);
  if (user_id) return redirect("/dashboard");
  return null;
};

export let action = async ({ request }) => {
  return await register(request);
};

export default function Register() {
  const toast = useActionData();
  return <Auth />;
}

© www.soinside.com 2019 - 2024. All rights reserved.