api(next.js,Open AI)"/>
无法获取localhost/api(next.js,Open AI)
每次运行目录的客户端时,都会收到错误消息:无法获取“http://localhost:8080/api/post”,这是目录的服务器端的一部分。服务器端似乎运行良好(带有消息:服务器已启动, MongoDB 连接)。
[服务器目录中我的index.js文件]
const app = express();
app.use(cors());
app.use(express.json({limit: '50mb'}));
app.use('/api/v1/post', postRoutes);
app.use('/api/v1/dalle', dalleRoutes);
app.get('/', async(req, res) => {
res.send('Hello from DALL-E!');
})
const startServer = async() => {
try {
connectDB(process.env.MONGODB_URL);
app.listen(8080, () => console.log('Server has started on port http://localhost:8080'))
}catch (error){
console.log(error);
}
};
[客户端页面似乎有问题]
const fetchPosts = async () => {
setLoading(true);
try {
const response = await fetch('http://localhost:8080/api/v1/post', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok){
const result = await response.json();
setAllPosts(result.data.reverse());
}
} catch(error){
alert(error)
} finally{
setLoading(false);
}
};
我安装了 Cors 并在服务器端的 py index.js 中实现了它。我不知道是什么导致了错误。我是使用 next.js 的新手,我自己无法找出错误。帮助将不胜感激。谢谢
回答如下:更多推荐
无法获取localhost/api(next.js,Open AI)
发布评论