向 Node-http-proxy Node.js 发布请求时套接字挂断 [英] Socket hangup while posting request to Node-http-proxy Node.js

查看:38
本文介绍了向 Node-http-proxy Node.js 发布请求时套接字挂断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Node.js 项目中工作,我希望 node 充当 solr 的代理

I'm working in Node.js Project and I want node to act as a proxy for solr

对于代理:我使用了 Node-http-proxy.问题是代理在获取请求的情况下工作出色,但在发布请求的情况下会导致套接字挂断异常

For proxy: I used Node-http-proxy. the problem is that proxy work excellent in case of get requests but in case of post requests it results in socket hang up exception

这是我的节点代码示例

var express = require('express');
var router = express.Router();

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

var proxyOptions = {
  host: "127.0.0.1",
  port: 8983
};
var proxy = httpProxy.createProxyServer(proxyOptions);

// It works excellent in GET request
router.get('/solr/*', function(req, res) {
  proxy.web(req, res, {
    target: 'http://' + proxyOptions.host + ':' + proxyOptions.port
  });
})

// the socket hang up in post request
router.post('/solr/*', function(req, res) {
  console.log('Post Request');
  proxy.web(req, res, {
    target: 'http://' + proxyOptions.host + ':' + proxyOptions.port
  });
})

这是节点控制台一段时间后的错误

And this is the error after some time in node console

Error: socket hang up
    at createHangUpError (http.js:1476:15)
    at Socket.socketOnEnd [as onend] (http.js:1572:23)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

任何有关问题原因的建议

Any suggestions about the cause of the problem

推荐答案

我认为问题出在中间件的顺序上.在 httpProxy 之前使用 bodyParser 会破坏带有 JSON body 的请求,因此应该在 bodyParser 之前使用 httpProxy.

I think the issue comes from the order of middleware. Using bodyParser before httpProxy will break the requests with JSON body, so httpProxy should be used before bodyParser.

您可能需要查看这个有关 bodyParser 的更多信息.

You may want to check this for more info about bodyParser.

这篇关于向 Node-http-proxy Node.js 发布请求时套接字挂断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆