javascript - 无法理解回调在 Connect 模块中的作用

标签 javascript node.js asynchronous callback node.js-connect

我正在阅读一本关于 NodeJs Connect 的书。关于 basicAuth 模块有这个小部分。我知道 basicAuth 现在已被弃用,但我无法理解这个简单的代码。书上说

Providing an asynchronous callback function

The final option is similar, except this time a callback is passed to basicAuth() with three arguments defined, which enables the use of asynchronous lookups. This is useful when authenticating from a file on disk, or when querying from a database.

Listing 7.7. A Connect basicAuth middleware component doing asynchronous lookups

enter image description here

没有其他信息。这就是关于在 basicAuth

中进行回调的全部内容

因此,代码获取用户名和密码。然后假设对象 User 有一个方法 authendicate 检查这个用户是否真的存在。完成后,调用 gotUser 函数。 gotUser 包含返回的错误(=未找到具有该用户名/密码的用户)或返回的用户对象(找到具有该用户名/密码的用户)。我说得对吗?

gotUser 检查是否有错误。如果存在,则返回并使用错误参数调用 callback。那么等等,此时 callback 会做什么?它没有在任何地方定义。它会将错误传递给错误处理函数吗?以及如何?

如果没有错误,gotUser 再次使用 null(= 无错误)和 user 调用 callback >。再一次,回调会做什么?为什么将返回的用户传递给回调而不获取其姓名、邮件、年龄等并在 session 中使用它们或填充标签或其他内容的 innerHTML

谢谢

最佳答案

So wait, what will callback do at this point? Its not defined anywhere.

callback 的值由basicAuth 中间件定义。

您可以在 basic-auth-connect module 中找到它的定义, 由 connect 使用, 在模块的 index.js :

callback(user, pass, function(err, user){
  if (err || !user)  return unauthorized(res, realm);
  req.user = req.remoteUser = user;
  next();
});

gotUser() 调用 callback(...) 时,它会调用 function(err, user){...}从上面的代码片段中,传递 err 和/或 user 以供使用。


还有,在您想知道的两种情况下,它们是如何使用的...

gotUser checks if there is an error. If there is, returns and calls callback with an error argument. So wait, what will callback do at this point?

If there is not an error, gotUser calls callback again with null(= no error) and user. Once again, what will callback do?

if (err || !user) 两个条件都将通过(一个有错误,另一个缺少用户)。然后它认为该请求未授权 并将立即结束响应。

function unauthorized(res, realm) {
  res.statusCode = 401;
  res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
  res.end('Unauthorized');
};

Why pass the returned user to the callback and not grab its name, mail, age etc etc and use them on a session or fill the innerHTML of a tag or whatever?

中间件正在申请separation of concerns , 保持自身尽可能小和简洁。它的目标只是确定一个 req.user 并验证它。

成功完成后,应用程序队列中的其他中间件将能够引用找到的用户。这可以包括使用它从 View 中呈现标记:

// determine the user
app.use(connect.basicAuth(...));

// now make use of it
app.use(function (req, res, next) {
  viewEngine.render('view', { user: req.user }, function (err, result) {
    if (err) return next(err);

    res.setHeader('Content-Type', 'text/html');
    res.end(result);
  });
});

注意:这是通用的,不会按原样运行。您需要找到并设置您选择的 View 引擎并将其替换到代码段中。


此外,关于...的旁注

fill the innerHTML of a tag

虽然 Node.js 正在执行 JavaScript,但它是在自己的环境中执行的,完全脱离任何浏览器。无法直接与用户当前看到的 DOM 进行交互。

关于javascript - 无法理解回调在 Connect 模块中的作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191310/

相关文章:

javascript - Select 不保留用户选择的值

javascript - for循环里面的闭包,里面的IIFE是做什么用的?

Javascript:返回 NAN 的实例方法

javascript - 通过 Mongoose (MongoDB) 中的嵌入引用数组查找文档

c# - 应该使用什么方法来并行执行一项任务多次?

c# - 在 ASP.NET MVC 中通过 jQuery 提交表单时传递参数

node.js - Nodejs 类验证器验证对象数组

javascript - 在 NodeJS 中测试对象相等性

javascript - 使用 Javascript 闭包将附加数据传递给 API 回调函数

java - Spring boot应用程序无法将 `Autowire`代理为新线程的Service类