首页 > 解决方案 > 如何从 localhost:5000 中托管的 WebApp 访问 localhost:3000 中托管的 WebApp?

问题描述

我有一个 NodeJS webapp 托管在localhost:5000和另一个 Flask webapp 托管在localhost:3000。我想从 NodeJS 应用程序中的路由访问托管在 localhost:3000 中的 html 文件。

我怎么做?

NodeJS 应用程序路由

router.get('/fetchpredict', (req, res) => {
  //In this route I want to fetch the HTML page hosted in localhost:3000
})

注意:我不想要 JSON 格式的数据。我想要在 localhost:3000 中呈现的 HTML 页面

标签: node.js

解决方案


您需要使用类似node-fetch的东西才能localhost:3000从您的路由内部/fetchpredict发送请求......您还需要使路由处理程序异步。

就像是:

const fetch = require('node-fetch');
// ...
router.get('/fetchpredict', async (req, res) => {
  try {
    const resp = await fetch("localhost:3000");
    const html = await resp.text();
    res.status(200).send(html);
  } catch (e) {
    res.status(500).send(e);
  }
})

推荐阅读