Next js api 不适用于 netlify

Next js api not works on netlify

我有一个用于下一个 js 的 Web 应用程序。我想把它放在 netlify 上,但是当我这样做时,api 停止为我工作,尽管在 vercel 上一切正常。当我调用 api 时出现 404 错误

package.json:

{
  "name": "devhub-website",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "export": "next export"
  },
  "dependencies": {
    "axios": "^0.19.2",
    "dotenv": "^8.2.0",
    "js-cookie": "^2.2.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.6.0",
    "next": "9.5.2",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "sass": "^1.26.10"
  },
  "devDependencies": {
    "@types/node": "^14.0.27",
    "@types/react": "^16.9.46",
    "typescript": "^3.9.7"
  }
}

如果您使用 next export 在 Netlify 中部署,API 路由将不起作用。当我们使用 next export 构建时,此警告显示在终端中:

Statically exporting a Next.js application via next export disables API routes. This command is meant for static-only hosts, and is not necessary to make your application static.

确保在 next.config.js

中将目标设置为无服务器

Next.js 无服务器目标从页面输出无服务器函数。

module.exports = {
  target: "serverless",
};