API响应:
export const getCourses = (req, res) => {
const q = `SELECT c.*, u.id AS teacherId FROM courses AS c JOIN users AS u ON (u.id = c.teacherId)`;
db.query(q, (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json(data);
});
};
axios请求功能:
export const makeRequest = axios.create({
baseURL: "http://localhost:8800/api/",
withCredentials: true,
headers: [{ "Access-Control-Allow-Origin": origin, "access-control-allow-credentials": true }],
method: "GET",
url: "requisitions",
});
前端请求(useQuery):
const { isLoading, error, data } = useQuery({
queryKey: ["courses"],
queryFn: () => makeRequest
.get("/courses")
.then((res) => {
return res.data;
}),
});
我收到的错误:GET http://localhost:8800/api/courses 404(未找到)
我尝试使用 Insomnia 发帖,并得到了回复。但在这里我收到了错误。
如何解决这个错误?
Fix your request handker like this
export const getCourses = app.get('/api/ciurses', (req, res) => {
const q = `SELECT c.*, u.id AS teacherId FROM courses AS c JOIN users AS u ON (u.id = c.teacherId)`;
db.query(q, (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json(data);
});
};)