Why this code doesn't work?
const whitelist = ['http://localhost:3000' , 'https://localhost:3443', 'http://localhost:4200','http://localhost:5000'];
const corsOptionsDelegate =(req: Request, callback: any) => {
let corsOptions;
console.log('origin' + req.header('Origin'));
if(req.header('Origin') != null){
if(whitelist.indexOf(req.header('Origin')) !== -1){
console.log("index : " + whitelist.indexOf(req.header('Origin')));
corsOptions = { origin : true};
}
else{
console.log('here else :' + whitelist.indexOf(req.header('Origin')));
corsOptions = { origin : false };
}
}
callback(null , corsOptions);
};
ERROR:
Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.
Error comes from (req.header('Origin')) parentheses.