简体   繁体   English

在 node.js 中使用的 exports.local 是什么

[英]What is exports.local used for in node.js

Exports.local Node js sample code Exports.local Node js 示例代码

I am using passport-local-mongoose in my node js Application and I come across exports.local for passport authentication.我在我的节点 js 应用程序中使用了 passport-local-mongoose,我遇到了 export.local 进行护照身份验证。 I couldn't understand it function.我无法理解它 function。 Please check the image above请检查上面的图片

In your case here there is nothing special about local keyword, it is just the name of the variable that is used to export the passport local authentication strategy configuration, so you can call it in other files using require , so here in your example, you have this logic written in authenticate.js , so to use it in any other file you will have to call it using the following:在您的情况下, local关键字没有什么特别之处,它只是用于导出护照本地身份验证策略配置的变量的名称,因此您可以使用require在其他文件中调用它,因此在您的示例中,您将此逻辑写在authenticate.js中,因此要在任何其他文件中使用它,您必须使用以下命令调用它:

const { local } =  require('./authenticate'); // identify the right path to authenticate.js
enter code here

The CommonJS (CJS) format is used in Node.js and uses require and module.exports to define dependencies and modules.在 Node.js 中使用 CommonJS (CJS) 格式,并使用 require 和 module.exports 来定义依赖项和模块。 The npm ecosystem is built upon this format. npm 生态系统建立在这种格式之上。 In your case exports.local creates a new module and export it for the use elsewhere.在您的情况下exports.local创建一个新模块并将其导出以供其他地方使用。

Example user.js示例 user.js

const getName = () => {
   return 'Jim';
};

exports.getName = getName;

index.js index.js

const user = require('./user');
console.log(`User: ${user.getName()}`);

Output Output

User: Jim用户:吉姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM