s4developer (3) [Avatar] Offline
#1
Hi,

I'd like to thank you for taking the time to write this book! it has provided great insight into CORS and how to implement it into my projects.

As I worked through the examples I struggle with one piece of code that I'd like some help on.

var createWhitelistValidator = function(whitelist) {
return function(val) { // when is this variable inside the function assigned a value ??
for (var i = 0; i < whitelist.length; i++) {
if (val === whitelist[i]) {
return true;
}
}
return false;
}
};

At your convenience please help me understand this.

Thanks,
-Rob
s4developer (3) [Avatar] Offline
#2
Re: Chapter 6 - Listing 6.2 Creating a white list validator function in app.js
Ok I think I found the connection:

Please correct me if I'm wrong but the answer to my earlier question is in handleCors:

var handleCors = function(options) {
return function(req, res, next) {

if (options.allowOrigin) {
var origin = req.headers['origin'];
if (options.allowOrigin(origin)) {
res.set('Access-Control-Allow-Origin', origin);
}
} else {
res.set('Access-Control-Allow-Origin', '*');
}

res.set('Access-Control-Allow-Credentials', 'true');
if (isPreflight(req)) {
res.set('Access-Control-Allow-Methods', 'GET, DELETE');
res.set('Access-Control-Allow-Headers',
'Timezone-Offset, Sample-Source');
res.set('Access-Control-Max-Age', '120');
res.status(204).end();
return;
} else {
res.set('Access-Control-Expose-Headers', 'X-Powered-By');
}
next();
}
};

I'm still a little fuzzy on it but i know the connection is within these two functions.
s4developer (3) [Avatar] Offline
#3
Re: Chapter 6 - Listing 6.2 Creating a white list validator function in app.js
Ok...i got figured out...only took 4 cups of coffee and 3 1/2 hours but I found the connection...my mistake was simple.

I was looking for a variable not a the value passed.