node.js - 如何从 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-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);
}
})
推荐阅读
- basic - Visual Basic 输出
- python-3.x - sqlite3.OperationalError:靠近“The”:语法错误/参数化 sql
- mongodb - Mongoose 通过不替换 ID 字段自动递增
- android - 在 Kotlin 中使用 Generic 时发现类型不匹配
- sql - 在 SQL 中将行更改为列
- java - 来自 Ubuntu 16 的 java 中的 ZeroMQ 绑定
- xml - XSLT:多次复制嵌套元素
- laravel-5 - 如何将多维数组转换为内爆值的索引数组?
- c# - 当时区不同时,发送 DateTime 的 WCF 服务的值会发生变化
- javascript - 表单提交成功消息显示后