gar*_*dos 8 couchdb node.js express
我使用请求在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);
});
Run Code Online (Sandbox Code Playgroud)
在进行GET请求时,它可以工作:请求从客户端到node.js再到CouchDB并成功返回.POST和PUT请求无限期挂起.日志语句一直运行到代理,但CouchDB不表示收到请求.为什么会发生这种情况,如何解决?
Express的bodyparser中间件以导致管道挂起的方式修改请求.不知道为什么,但你可以通过将代理放入在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());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1112 次 |
| 最近记录: |