I am following a NodeJS tutorial that I saw the following code, that I can't understand what will this module export and what will someone get in cors
when he/she does var cors = require('./cors')
?
const express = require('express');
const cors = require('cors');
const app = express();
const whitelist = ['http://localhost:3000', 'https://localhost:3443'];
var corsOptionsDelegate = (req, callback) => {
var corsOptions;
console.log(req.header('Origin'));
if(whitelist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true };
}
else {
corsOptions = { origin: false };
}
callback(null, corsOptions);
};
exports.cors = cors();
exports.corsWithOptions = cors(corsOptionsDelegate);
Node.js file exports the exports
object. In this example, this object will consist of two fields: cors
and corsWithOptions
, with their respective values as were in their assignments.
I assume that your question refers to the situation where this file is saved as cors.js
. So a file which imports this file via var cors = require('./cors')
would actually assign that same exports
object into the cors
variable.
To illustrate, if this would be the code in the importing file:
var cors = require('./cors');
console.log(cors)
Then this will be its output:
{ cors: [Function: corsMiddleware],
corsWithOptions: [Function: corsMiddleware] }
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.