我正在开发一个使用 Google reCAPTCHA 的 React/Next 项目。我的前端似乎在工作(我知道,因为我一路上设置了打印语句),但后端在我的本地终端中给我这个错误: 错误 - 没有在“src pp pi”中导出 HTTP 方法 验证码 oute.ts'。为每个 HTTP 方法导出命名导出。
我的开发工具也出现错误:'POST http://localhost:3000/api/recaptcha 405 (Method Not Allowed)' 我认为这与另一个错误有关。
代码如下:
import { NextApiRequest, NextApiResponse } from 'next';
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import axios from 'axios';
const app = express();
app.use(cors());
app.use(bodyParser.json());
console.log('hi');
export async function postHandler(req: NextApiRequest, res: NextApiResponse){
if (req.method === 'POST') {
const { token } = req.body;
try {
const response = await axios.post(
`https://www.google.com/recaptcha/api/siteverifysecret=${process.env.NEXT_PUBLIC_RECAPTCHA_SECRET_KEY}&response=${token}`
);
console.log('you made it here');
if (response.data.success) {
res.status(200).json({ message: 'reCAPTCHA verification successful' });
} else {
res.status(400).json({ message: 'reCAPTCHA verification failed' });
}
} catch (err) {
console.log(err);
res.status(500).json({ message: 'Internal server error' });
}
};
}
我试过重命名函数,将其导出为 const,并在文件末尾而不是在命名时导出。