对CouchDB的反向代理在Node.js中挂起POST和PUT

我使用请求在Express中实现以下反向代理到CouchDB:

app.all(/^\/db(.*)$/, function(req, res){ var db_url = "http://localhost:5984/db" + req.params[0]; req.pipe(request({ uri: db_url, method: req.method })).pipe(res); }); 

在进行GET请求时,它将起作用:请求从客户端转到node.js到CouchDB并成功返回。 POST和PUT请求无限期挂起。 日志语句运行直到代理,但CouchDB不表示收到请求。 为什么会发生这种情况,如何解决?

Express“bodyparser中间件修改请求的方式导致pipe道挂起。 不知道为什么,但是您可以通过将您的代理转换为在bodyparser之前捕获的中间件来修复它。 喜欢这个:

 // wherever your db lives var DATABASE_URL = 'http://localhost:5984/db'; // middleware itself, preceding any parsers app.use(function(req, res, next){ var proxy_path = req.path.match(/^\/db(.*)$/); if(proxy_path){ var db_url = DATABASE_URL + proxy_path[1]; req.pipe(request({ uri: db_url, method: req.method })).pipe(res); } else { next(); } }); // these blokes mess with the request app.use(express.bodyParser()); app.use(express.cookieParser()); 

请求默认获取请求。 你需要设置方法。

 app.all(/^\/db(.*)$/, function(req, res){ var db_url = ["http://localhost:5984/db", req.params[0]].join('/'); req.pipe(request({ url: db_url, method: url.method })).pipe(res); }); 

(代码未经testing,让我知道如果它不工作,但它应该是接近)