post 请求中的响应未定义(Express)

Response is undefined in post request (Express)

我有一个路由处理程序,当我转到指定路由时,它会调用身份验证控制器中的注册函数:

module.exports = app => {
  app.post("/signup", authentication.signup());
};

我的授权控制器:

exports.signup = function(req, res, next) {
  res.send({ success: true });
};

但是当我启动服务器时,我说它无法读取未定义的属性 "send"。作为一个甚至 运行 我的服务器,我不能使用邮递员来测试我的 API 路由。

为什么它甚至在我访问路由之前就尝试调用发送?我正在 运行 将我的服务器与我的 express 应用程序一起使用节点 http 库。

app.post("/signup", authentication.signup()); 调用 authentication.signup 并将其 return 值传递给 app.post,与 foo(bar()) 的方式完全相同 调用 bar 并将其 return 值传递给 foo.

相反,只需将函数本身传递给 app.post; Express 稍后将调用它以响应该路线上的 [​​=26=]:

module.exports = app => {
  app.post("/signup", authentication.signup);
  // No () here ---------------------------^
};