简体   繁体   中英

What is exports.local used for in node.js

Exports.local Node js sample code

I am using passport-local-mongoose in my node js Application and I come across exports.local for passport authentication. I couldn't understand it 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:

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. The npm ecosystem is built upon this format. In your case exports.local creates a new module and export it for the use elsewhere.

Example user.js

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

exports.getName = getName;

index.js

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

Output

User: Jim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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