小编dom*_*ard的帖子

在node-http-proxy中的中间件中调用异步方法

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

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

httpProxy.createServer(
require('./example-middleware')(),
9005, 'localhost'
).listen(8005)
Run Code Online (Sandbox Code Playgroud)

中间件模块的作用是使用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 …
Run Code Online (Sandbox Code Playgroud)

asynccallback node.js node-http-proxy

7
推荐指数
1
解决办法
1654
查看次数

标签 统计

asynccallback ×1

node-http-proxy ×1

node.js ×1