【问题标题】:What does ".Strategy" do in Node or Passport?“.Strategy”在 Node 或 Passport 中有什么作用?
【发布时间】:2019-04-13 23:30:10
【问题描述】:

“.Strategy”在这里做什么?是节点吗?是护照吗?

var LocalStrategy = require('passport-local').Strategy;

我理解“.Strategy”部分的所有内容。我只想知道'.Strategy'是做什么的。我检查了passport-local module on npm 上的文档。我还检查了 Passport 的文档,它只是在代码 sn-ps 中使用。没有提供任何解释。

我正在使用 MEAN 堆栈,我们正在使用 Passport 对用户进行身份验证。

【问题讨论】:

  • 你看npmjs.com/package/passport-local了吗?搜索“护照策略”?我似乎找到了相当多的文档。
  • 是的,这是我看的第一个地方。它并没有完全告诉我“.Strategy”做了什么。我理解“.Strategy”部分的所有内容。
  • 什么意思?你可以看到实现,它是开源的。或者看抽象版:github.com/jaredhanson/passport-strategy
  • 我不知道查看本地策略源代码,因为文档中没有关于它的任何内容。据我所知,它可能是 Node 或 Passport。
  • 但是您可以在需要时查看它的来源,您还需要哪些其他信息才能找到它?你可以在索引文件中看到它来自于 strategy.js,这似乎是合乎逻辑的。

标签: node.js passport.js mean-stack


【解决方案1】:

如果您查看passport-local index.js 的来源,您会看到它直接在exports.Strategy 中导出相同的内容。

当您执行require('passport-local).Strategy 时,您导入了在exports.Strategy 中定义的导出,但在这种情况下只执行require('passport-local') 实际上是相同的,因为相同的构造函数是直接从模块中导出的。

如果你这样定义一个模块:

var Thing = { foo: () => 'bar' };

exports = module.exports = Thing;

exports.Thing = Thing;

您可以通过多种方式使用它:

const Thing = require('./module');
console.log(Thing.foo());

工作正常

const Thing = require('./module').Thing;
console.log(Thing.foo());

并且通过这两个导入,您实际上也可以调用

console.log(Thing.Thing.foo());

如果您删除模块的exports.Thing = Thing; 部分,那么

const Thing = require('./module').Thing;

不再起作用了。

出口经常引起混乱。您可以查看Node docs 或例如。 this answer.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-16
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2020-01-18
    • 2014-01-05
    • 2020-11-10
    相关资源
    最近更新 更多