http-proxy继续返回404

彼得·帕夫利克

我正在尝试使用http-proxy将调用代理到REST API,但是它一直返回404代码。

这是对我的API的示例调用:http : //petrpavlik.apiary-mock.com/notes它返回一些JSON数据。

我的服务器代码如下所示:

var httpProxy = require('http-proxy');

var proxy = httpProxy.createServer({
  target:'http://petrpavlik.apiary-mock.com'
});

proxy.listen(8005);

proxy.on('error', function (err, req, res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });

  res.end('Something went wrong. And we are reporting a custom error message.');
});

proxy.on('proxyRes', function (res) {
  console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
});

这是我尝试使用代理调用相同请求时得到的信息。

Petrs-MacBook-Air-2:~ petr$ curl -v 127.0.0.1:8005/notes
* About to connect() to 127.0.0.1 port 8005 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x7f977280fe00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f977280fe00) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8005 (#0)
> GET /notes HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 127.0.0.1:8005
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< cache-control: no-cache, no-store
< content-type: text/html; charset=utf-8
< date: Sat, 28 Jun 2014 09:40:29 GMT
* Server MochiWeb/1.0 (Any of you quaids got a smint?) is not blacklisted
< server: MochiWeb/1.0 (Any of you quaids got a smint?)
< content-length: 2960
< connection: Close
< 

我肯定想念一些明显的东西,但是我真的被这东西卡住了。

gpbl

如果您尝试代理Apiary Mock API,则可以通过以下方式完成:

var apiProxy = httpProxy.createProxyServer();
server.get("/api/v1", function (req, res) {
  delete req.headers.host;
  req.url = req.url.replace('/api/v1', '/');
  apiProxy.web(req, res, { target: 'http://private-XXXX.apiary-mock.com/' });
});

现在,在我的本地应用中,我要求GET /api/v1/notes被代理http://private-XXXX.apiary-mock.com/notes

确保您为delete req.headers.host,否则目标服务器将匹配错误的主机。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

http-proxy不断返回404

来自分类Dev

我如何从dropwizard返回404 http状态

来自分类Dev

如何从SPA返回HTTP 404状态代码?

来自分类Dev

Simple Rest Webservice返回http状态404

来自分类Dev

HTTP POST方法返回状态码404

来自分类Dev

找不到Akka Http返回404

来自分类Dev

AngularJS http.get返回404

来自分类Dev

Python HTTP请求返回404或字节

来自分类Dev

Python HTTP请求返回404或字节

来自分类Dev

Python HTTP请求返回404或字节

来自分类Dev

Python HTTP请求返回404或字节

来自分类Dev

Web API HttpClient PutAsync返回Http 404

来自分类Dev

AngularJS $ http.post()返回404

来自分类Dev

找不到Akka Http返回404

来自分类Dev

返回HTTP响应并继续处理-Python Bottle API

来自分类Dev

json文件的$ http.get始终返回404

来自分类Dev

我应该返回501/505 HTTP状态还是400/404

来自分类Dev

Golang Gorilla Mux与http.FileServer返回404

来自分类Dev

Grails SubFlow不呈现其视图页面并返回HTTP 404

来自分类Dev

返回的正确HTTP错误代码是404 vs 412?

来自分类Dev

Grails SubFlow不呈现其视图页面并返回HTTP 404

来自分类Dev

AngularJs:$ http.post总是返回404错误

来自分类Dev

即使服务返回 404,$http promise 也会执行成功块

来自分类Dev

HTTP 404 Java Servlet

来自分类Dev

角度 $http 404 错误

来自分类Dev

http 处理 404 错误

来自分类Dev

HTTP 状态 404 -

来自分类Dev

未找到 Http 404

来自分类Dev

如何在球衣中为空值返回HTTP 404而不是HTTP 204响应代码