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

问题描述

我正在Node.js项目工作,我希望节点作为solr的代理

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

对于代理:我使用。
的问题是,代理工作出色的情况下获得请求,但如果发布请求,它导致插座挂起异常

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

这是一个我的样本节点代码

Here is a sample of my node code

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.

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

07-31 22:03