【发布时间】:2021-05-21 22:27:10
【问题描述】:
我正在尝试将我的 nuxt 应用程序部署到 Vercel 上,但每当我尝试发出 api 请求时,都会不断收到“Cannot GET /api/hello”或任何其他端点。我在日志中看不到任何错误,它返回 404 状态。一切都在我的本地环境中完美运行。
vercel.json:
{
"version": 2,
"rewrites": [
{ "source": "/api/(.*)", "destination": "/api" }
],
"builds": [
{
"src": "api/**/*.js",
"use": "@vercel/node"
},
{
"src": "nuxt.config.js",
"use": "@nuxtjs/vercel-builder",
"config": {
"serverFiles": ["api/**"]
}
}
]
}
nuxt.config.js
// Server Middleware API
serverMiddleware:
process.env.NODE_ENV === 'production'
? []
: [
{ path: '/api', handler: '~/api/index' },
{ path: '/api/hello', handler: '~api/hello' }
],
/api/hello.js
import express from 'express'
import bodyParser from 'body-parser'
const app = express()
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.status(200).json('test - get').end()
})
app.post('/', (req, res) => {
res.status(200).json('test - post').end()
})
export default app
【问题讨论】:
-
构建是否在本地工作?
-
是的,本地一切正常