簡體   English   中英

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

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

Exports.local Node js 示例代碼

我在我的節點 js 應用程序中使用了 passport-local-mongoose,我遇到了 export.local 進行護照身份驗證。 我無法理解它 function。 請檢查上面的圖片

在您的情況下, local關鍵字沒有什么特別之處,它只是用於導出護照本地身份驗證策略配置的變量的名稱,因此您可以使用require在其他文件中調用它,因此在您的示例中,您將此邏輯寫在authenticate.js中,因此要在任何其他文件中使用它,您必須使用以下命令調用它:

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

在 Node.js 中使用 CommonJS (CJS) 格式,並使用 require 和 module.exports 來定義依賴項和模塊。 npm 生態系統建立在這種格式之上。 在您的情況下exports.local創建一個新模塊並將其導出以供其他地方使用。

示例 user.js

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

exports.getName = getName;

index.js

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

Output

用戶:吉姆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM