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