node.js - http-proxy 错误:必须提供正确的 URL 作为目标
问题描述
这是我第一次尝试 http-proxy。我收到错误消息:
Error: Must provide a proper URL as target
代码:
httpProxy = require('http-proxy');
httpProxy.createServer(function(req, res) {
proxy.web(req, res, { target: 'http://127.0.0.1:10002' });
}).listen(3001);
我从 curl 知道我的网站在端口 10002 上运行。
正确的 URL 是什么样的?还是这个错误实际上意味着完全不同的东西?开箱即用无法访问端口 10002,我正在通过 Internet 进行测试。
解决方案
您需要使用http
模块创建服务器,而不是http-server
.
替换httpProxy.createServer
为require('http').createServer
:
const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer({}) // I added this line to make this full snippet working
require('http').createServer(function (req, res) {
proxy.web(req, res, { target: 'http://127.0.0.1:10002' })
}).listen(3001)
推荐阅读
- flutter - Flutter build runner - 通过命令行启用/禁用构建器
- apache - 403 禁止:您无权访问此资源。Apache/2.4.29 (Ubuntu) 服务器端口 443
- javascript - 从方法读取 vuejs 生产中的 css 文件的问题
- javascript - 如何根据条件显示表单组件?
- jquery - 将 intit 代码从 document.ready() 移动到用户定义的方法后,数据表初始化不起作用
- twilio - 代理如何将数据传递给任务路由器?
- linux-kernel - linux内核中“trace_gpio_value”函数的定义
- firebase - 如何将 StreamBuilder 与模型一起使用并从 firestore 获取数据?
- numpy - 使用点绘制线性回归线
- python - 如何从另一个 Python 文件中获取值?