Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 快速应用程序。获取给定的数组_Javascript_Express - Fatal编程技术网

Javascript 快速应用程序。获取给定的数组

Javascript 快速应用程序。获取给定的数组,javascript,express,Javascript,Express,我正在阅读演示使用的代码,该代码可用于实现OAuth2授权服务器 不过,我问的问题并不奇怪。我只是对Express 3.x的基础知识有点问题 在app.js中: oauth2 = require('./oauth2') . . . app.get('/dialog/authorize', oauth2.authorization); 在Oauth2.js中: exports.authorization = [ login.ensureLoggedIn(), server.a

我正在阅读演示使用的代码,该代码可用于实现OAuth2授权服务器

不过,我问的问题并不奇怪。我只是对Express 3.x的基础知识有点问题

在app.js中:

oauth2 = require('./oauth2')
. . . 
app.get('/dialog/authorize', oauth2.authorization);
在Oauth2.js中:

exports.authorization = [ 
    login.ensureLoggedIn(),
    server.authorization(function (clientID, redirectURI, scope, done) {
        db.clients.findByClientId(clientID, function (err, client) {
            if (err) {
                return done(err);
            }   
            if(client) {
                client.scope = scope;
            }   
            // WARNING: For security purposes, it is highly advisable to check that
            //          redirectURI provided by the client matches one registered with
            //          the server.  For simplicity, this example does not.  You have
            //          been warned.
            return done(null, client, redirectURI);
        }); 
    }), 
    function (req, res, next) {
        //Render the decision dialog if the client isn't a trusted client
        //TODO Make a mechanism so that if this isn't a trusted client, the user can recorded that they have consented
        //but also make a mechanism so that if the user revokes access to any of the clients then they will have to
        //re-consent.
        db.clients.findByClientId(req.query.client_id, function(err, client) {
            if(!err && client && client.trustedClient && client.trustedClient === true) {
                //This is how we short call the decision like the dialog below does
                server.decision({loadTransaction: false}, function(req, callback) {
                    callback(null, { allow: true }); 
                })(req, res, next);
            } else {
                res.render('dialog', { transactionID: req.oauth2.transactionID, user: req.user, client: req.oauth2.client }); 
            }
        });
    }   
];
那么,这是因为
app.get()
可以使用一系列中间件吗?我正试图找到app.get()的代码所在位置,但找不到它

编辑: 我坐的是3.6特快。所以根据推断的答案,如果我错了,请纠正我。
你是说oauth2.authorization数组而不是模块

app.VERB转到
this.\u router[方法].apply(this.\u router,参数)

其中,arguments是一个类似数组的对象,只有一项,即
oauth2.authorization
数组

然后转到以下定义的函数中的router/index.js:

methods.forEach(function(method){
  Router.prototype[method] = function(path){
    var args = [method].concat([].slice.call(arguments));
    this.route.apply(this, args);
    return this;
  };  
});
在这里,以前是
参数
的现在是
路径
。然后变成args。因此,
oauth2.authorization
给出的原始数组仍然存在,并且
args
中有一个长度为2的项,第一个项是方法名“get”,第二个是数组

此.route在同一文件中定义:

Router.prototype.route = function(method, path, callbacks){
  var method = method.toLowerCase()
    , callbacks = utils.flatten([].slice.call(arguments, 2));

  // ensure path was given
  if (!path) throw new Error('Router#' + method + '() requires a path');

  // ensure all callbacks are functions
  callbacks.forEach(function(fn){
    if ('function' == typeof fn) return;
    var type = {}.toString.call(fn);
    var msg = '.' + method + '() requires callback functions but got a ' + type;
    throw new Error(msg);
  }); 

  // create the route
  debug('defined %s %s', method, path);
  var route = new Route(method, path, callbacks, {
    sensitive: this.caseSensitive,
    strict: this.strict
  }); 

  // add it
  (this.map[method] = this.map[method] || []).push(route);
  return this;
};

因为存在
utils.flant([].slice.call(参数,2))
oauth2.authorization
中的数组被展平。因此,发送的东西似乎不是数组,而是普通参数。(我不知道“2”在做什么)。oauth2.authorization的第三个回调函数很容易理解。第一个是
login.ensureLoggedIn()
哪个是中间件?第二个是
server.authorization()
。但我不完全确定它在做什么。

对于
get
方法,在第一个参数之后,应用程序将添加路由,然后将其他参数传递给相关控制器

this._router[method].apply(this._router, arguments);
app.js

app.get('/', routes.index);
// controller
exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};
methods.forEach(function(method){
  app[method] = function(path){
    if ('get' == method && 1 == arguments.length) return this.set(path);

    // deprecated
    if (Array.isArray(path)) {
      console.trace('passing an array to app.VERB() is deprecated and will be removed in 4.0');
    }

    // if no router attached yet, attach the router
    if (!this._usedRouter) this.use(this.router);

    // setup route
    this._router[method].apply(this._router, arguments);
    return this;
  };
});
index.js

app.get('/', routes.index);
// controller
exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};
methods.forEach(function(method){
  app[method] = function(path){
    if ('get' == method && 1 == arguments.length) return this.set(path);

    // deprecated
    if (Array.isArray(path)) {
      console.trace('passing an array to app.VERB() is deprecated and will be removed in 4.0');
    }

    // if no router attached yet, attach the router
    if (!this._usedRouter) this.use(this.router);

    // setup route
    this._router[method].apply(this._router, arguments);
    return this;
  };
});
application.js

app.get('/', routes.index);
// controller
exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};
methods.forEach(function(method){
  app[method] = function(path){
    if ('get' == method && 1 == arguments.length) return this.set(path);

    // deprecated
    if (Array.isArray(path)) {
      console.trace('passing an array to app.VERB() is deprecated and will be removed in 4.0');
    }

    // if no router attached yet, attach the router
    if (!this._usedRouter) this.use(this.router);

    // setup route
    this._router[method].apply(this._router, arguments);
    return this;
  };
});
所以

对于
/对话框/authorize
视图,将传递由
oauth2.authorization
模块导出的
authorization
方法

编辑

我不确定阵列导出,请尝试以下方法:


你是说oauth2.authorization数组而不是模块?你能检查我的编辑吗?@huggie你有没有试过直接传递参数而不是导出数组?这实际上不是我的代码。这是来自Oauth2orizeRecipes的工作代码,我正试图理解它是如何工作的。我明白了。它所做的工作与
oauth2orize
文档中编写的内容非常相似。无论是否使用数组,它都必须是相同的。现在我只需要理解它在做什么,这将是另一个问题。非常感谢你的帮助,我很感激。