Express js委托http动词

时间:2015-11-23 08:52:51

标签: javascript express

我在快递应用程序中的以下行设置了一个断点。

app.get('/api/post/:id', api.post);

当我进入该功能时,它会转到:

/**
 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.
 */

methods.forEach(function(method){
  app[method] = function(path){
    if (1 == arguments.length) return this.routes.lookup(method, path);
    var args = [method].concat(toArray(arguments));
    if (!this.__usedRouter) this.use(this.router);
    return this.routes._route.apply(this.routes, args);
  }
});

任何人都可以解释发生了什么吗?它是如何以及为何直接针对这段代码?

1 个答案:

答案 0 :(得分:0)

已发布的代码位是app.get函数的内容声明。

methods将是一组动词(getpostput等),并且每个动词都在app上声明为函数。

我认为你真正感兴趣的是api.post函数。要检查您是否必须在该函数中放置断点,或者单步执行此行apply上的this.routes._route.apply(this.routes, args);以查看接下来会发生什么。