我有一个api文件夹在我的next.js应用程序的一些服务器端端点:
import { NextApiRequest, NextApiResponse } from 'next'
import Cors from 'cors'
// Initializing the cors middleware
const cors = Cors({
methods: ['GET', 'HEAD', 'POST'],
origin: '*',
optionsSuccessStatus: 200,
})
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
export default async (req, res) => {
await runMiddleware(req, res, cors)
const POSKEY = process.env.POSKEY
const PAYEE = process.env.PAYEE
const { currency, url, locale, price } = req.body
const currentUrl = url
const apiResult = await fetch(
'https://api.test.barion.com/v2/Payment/Start',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': 3495,
},
body: JSON.stringify({
PosKey: POSKEY,
PaymentType: 'Immediate',
GuestCheckout: true,
FundingSources: ['All'],
Currency: currency,
RedirectUrl: currentUrl,
CallbackUrl: currentUrl,
Locale: locale,
Transactions: [
{
Payee: PAYEE,
Total: price,
Items: [
{
Name: 'Teszt',
Description: 'Test item comment',
Quantity: 1,
Unit: 'pc',
UnitPrice: 1,
ItemTotal: 1,
SKU: 'SM-01',
},
],
},
],
}),
}
)
.then((result) => {
return result.json()
})
.catch((error) => {
console.error(error)
})
res.status(200).json({ url: apiResult.GatewayUrl })
}
当我调用端点时,在开发中它工作得很好:
但是在生产中我得到了500个错误。(部署到vercel)
vercel控制台错误:
[POST]/apigateway/23:30:28:532022-06-27T21:30:28.595Z e8c57750-4647-4e7a-b62e-6221abc141ac错误错误:无法找到模块'/var/task/node_modules/next/dist/server/next.js'。请确认包裹。Json有一个有效的"main";条目at tryPackage (internal/modules/cjs/loader.js:321:19)在Function.Module。_findPath(内部/模块/cj/loader.js: 534:18)在Function.Module。_resolveFilename(内部/模块/cj/loader.js: 888:27)在Function.Module。_load(内部/模块/cj/loader.js: 746:27)在模块。需要(内部/模块/cj/loader.js: 974:19)在require (internal/modules/cjs/helpers.js:101:18)/var/task/.next/server/pages/api/gateway.js:20:39在webpack_require(/var/任务/. next/服务器/webpack-api-runtime.js: 25:42)在webpack_exec(/var/任务/. next/服务器/页面/api/gateway.js: 109:39)在/var/task/.next/server/pages/api/gateway.js:110:28 {code: 'MODULE_NOT_FOUND',路径:'/var/任务/node_modules/下/包。json', requestPath: 'next'}RequestId: e8c57750-4647-4e7a-b62e-6221abc141ac错误:Runtime exiterror: exit status 1运行时。ExitError
我应该添加哪些其他配置到我的下一个。配置文件使其工作,我是初学者使用这个api。
更新:这解决了我的问题…https://github.com/vercel/next.js/issues/34844
问题是.js文件导入错误:同样的问题:https://github.com/vercel/next.js/issues/34844#issuecomment-1055628706
TLDR:从"next";
中删除导入{NextApiRequest, NextApiResponse}