无法获取localhost/api(next.js,Open AI)

编程入门 行业动态 更新时间:2024-10-03 04:39:32

无法获取localhost/<a href=https://www.elefans.com/category/jswz/34/1771441.html style=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)

本文发布于:2024-05-31 06:33:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:api   localhost   js   AI   Open

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!