http-proxy错误:必须提供正确的URL作为目标

时间:2019-12-07 23:18:18

标签: node.js http-proxy

这是我第一次尝试使用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进行测试。

1 个答案:

答案 0 :(得分:1)

您需要使用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)