无法 link 后端 API 路由 - NextJS

Unable to link backend API routes - NextJS

我正在制作一个 shopify 应用程序,并在前端创建了一个 Koa + NodeJS 后端和 NextJS 运行,在同一端口,我还创建了一个自定义 server.js 文件当 运行 在我的 PC 本地,它 运行 没问题,所有路由都按预期工作,我能够从后端 (Koa) 中定义的路由从前端 (React) 获取数据。

现在的问题是,当我将我的应用程序部署到 Vercel 时,它似乎无法识别我的 Koa 路由,并且针对我向后端路由发出的所有请求在控制台中抛出 404 错误。这是我第一次使用 NextJS,所以我真的不知道这里出了什么问题,所以我希望得到一些支持。

此外,当我部署它时,shopify 身份验证似乎也不再像在本地开发中 运行 时那样工作。

我的代码: server.js:

require('isomorphic-fetch');

const dotenv = require('dotenv');
dotenv.config();

const Koa = require('koa');
const next = require('next');
const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
const { verifyRequest } = require('@shopify/koa-shopify-auth');
const session = require('koa-session');
const { default: graphQLProxy } = require('@shopify/koa-shopify-graphql-proxy');
const { ApiVersion } = require('@shopify/koa-shopify-graphql-proxy');
const Router = require('koa-router');
const { receiveWebhook, registerWebhook } = require('@shopify/koa-shopify-webhooks');
const getSubscriptionUrl = require('./server/getSubscriptionUrl');

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

const {
  SHOPIFY_API_SECRET_KEY,
  SHOPIFY_API_KEY,
  HOST,
} = process.env;

app.prepare().then(() => {
  const server = new Koa();
  const router = new Router();
  server.use(session({ sameSite: 'none', secure: true }, server));
  server.keys = [SHOPIFY_API_SECRET_KEY];

  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      scopes: ['read_products', 'write_products'],
      async afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set("shopOrigin", shop, {
          httpOnly: false,
          secure: true,
          sameSite: 'none'
        });
        const registration = await registerWebhook({
          address: `${HOST}/webhooks/products/create`,
          topic: 'PRODUCTS_CREATE',
          accessToken,
          shop,
          apiVersion: ApiVersion.October19
        });

        if (registration.success) {
          console.log('Successfully registered webhook!');
        } else {
          console.log('Failed to register webhook', registration.result);
        }
        await getSubscriptionUrl(ctx, accessToken, shop);
      }
    })
  );

router
  .get('/api', ctx => {
    ctx.res.statusCode = 200;  
    ctx.body = "API Route"
  })

  const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });

  router.post('/webhooks/products/create', webhook, (ctx) => {
    console.log('received webhook: ', ctx.state.webhook);
  });

  server.use(graphQLProxy({ version: ApiVersion.April19 }));

  router.get('*', verifyRequest(), async (ctx) => {
    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
  });

  server.use(router.allowedMethods());
  server.use(router.routes());

  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
  });
});

我发现 Vercel 不再支持自定义服务器,如果我没记错的话。来自这个https://github.com/vercel/next.js/issues/9397#issuecomment-556215227

所以,我改用 Heroku 来部署我的应用程序,现在一切正常。