7

我正在尝试在 Node.js 中使用 node-http-proxy 创建一个代理,以检查请求是否在 mongodb 中获得授权。

基本上,我为我使用的 node-http-proxy 创建了一个中间件模块,如下所示:

httpProxy.createServer(
require('./example-middleware')(),
9005, 'localhost'
).listen(8005)

中间件模块所做的是使用 mongojs 连接到 mongodb 并运行查询以查看用户是否有权访问资源:

module.exports = function(){
// Do something when first loaded!
console.log("Middleware loaded!");

return function (req, res, next) {
var record = extractCredentials(req);
var query = -- Db query --

//Debug:
log("Query result", query);

db.authList.find(query).sort({
    "url.len": -1
}, function(err, docs){
    console.log(docs);

    // Return the creator for the longest matching path:
    if(docs.length > 0) {
        console.log("User confirmed!");
        next();
    } else {
        console.log("User not confirmed!");
        res.writeHead(403, {
            'Content-Type': 'text/plain'
        });
        res.write('You are not allowed to access this resource...');
        res.end();
    }

});

}
}

现在的问题是,一旦我使用 mongojs 添加对 mongodb 的异步调用,代理就会挂起并且永远不会发回响应。

澄清一下:在“用户未确认”上,一切正常,并返回 403。但是,在“用户确认”上,我看到了日志,但浏览器然后永远挂起并且请求没有被代理。

现在,如果我在回调之外删除“用户确认”和 next() 部分,它确实有效:

module.exports = function(){
// Do something when first loaded!
console.log("Middleware loaded!");

return function (req, res, next) {
    var record = extractCredentials(req);
    var query = --- query ---


    console.log("User confirmed!");
    next();
}

但我不能这样做,因为 mongojs 查询意味着(我猜是正确的)异步执行,只有当数据库回复时才会触发回调......

我也尝试了不使用中间件的版本:

http.createServer(function (req, res) {
  // run the async query here!
  proxy.proxyRequest(req, res, {
  host: 'localhost',
  port: 9000
});
}).listen(8001);

但这也无济于事......

有什么线索吗?请注意,我是 node.js 的新手,所以我怀疑我这边有误解......

4

2 回答 2

6

找到了答案,实际上问题是请求需要被缓冲:

httpProxy.createServer(function (req, res, proxy) {
// ignore favicon
if (req.url === '/favicon.ico') {
    res.writeHead(200, {
        'Content-Type': 'image/x-icon'
    } );
    res.end();
    console.log('favicon requested');
    return;
}

var credentials = extractCredentials(req);
console.log(credentials);

var buffer = httpProxy.buffer(req);

checkRequest(credentials, function(user){
    if(user == ...) {
        console.log("Access granted!");
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 9005, 
            buffer: buffer
        });
    } else {
        console.log("Access denied!");
        res.writeHead(403, {
            "Content-Type": "text/plain"
        });
        res.write("You are not allowed to access this resource...");
        res.end();
    }

});

}).listen(8005);
于 2012-07-30T10:48:32.507 回答
2

两个问题:

  1. 在你的回调next();else情况下你没有打电话。sort
  2. sort回调的第二个参数是一个Cursor,而不是文档数组。因此,docs.length > 0永远不会正确,代码始终遵循else路径。
于 2012-07-26T15:48:30.087 回答