我的获取操作耗时太长,直到失败
我试过铬合金,边缘和 Postman
来自pixabayapi的其他获取请求工作良好
我把代码和我做过的其他项目进行了比较,没有发现什么
我还按照某些地方的建议在“package.json”中添加了一个代理,但它不起作用
下面是我的代码:
控制器:
import axios from 'axios'
export const getAll = async () =>{
const response = await axios.get('https://pixabay.com/api/?key=25540812-faf2b76d586c1787d2dd02736')
.then(resp=>resp)
.catch(err=>console.log(err));
return response;
}
路由器:
import express from "express";
import * as homeController from '../controllers/home.controller.js'
const homeRouter = express.Router();
homeRouter.get('/all', homeController.getAll)
export default homeRouter
索引:
import express from "express"
import dotenv from "dotenv"
import homeRouter from './routers/home.router.js'
dotenv.config();
const PORT = 3000 //process.env.PORT
console.log(PORT);
const app = express();
app.use(express.json());
app.use(homeRouter)
app.listen(PORT, ()=>{console.log(`server is connected on port ${PORT}`)})
获取:
const getAll = async()=>{
try {
const response = await fetch (`http://localhost:3000/all`)
console.log("hu");
if (!response.ok) {
throw new Error();
}
else{
console.log("ok");
}
const responseObj = await response.json();
console.log(responseObj);
}
catch (error) {
console.log(error);
}
}
useEffect(()=>{
getAll();
},[])
1条答案
按热度按时间sr4lhrrt1#
@Jaromanda X把答案发出来给大家看:
“看这个
app.get('/', (req, res) => {
......你在哪里req
和res
???哪里都没有,那就是-提示:export const getAll = async (req, res) =>{
英寸显然,用express制作的每个控制器都需要发回一个响应(以res.send的形式)
应该很明显,但不知怎么的我错过了
谢谢大家!