NodeJS Parseing RegEx from environmental variable

Issue

This Content is from Stack Overflow. Question asked by Damiisdandy

I am using the node package cors, and I’d like to pass regular expressions into the allowed origins. Still, since I am hosting my node application on multiple servers for “production” and “staging”, I am using environmental variables to store the origins. Still, when env variables are parsed they get returned as strings.

Here is an example of my env variable

CORS_ORIGINS=/.example.com$/,/.example.app$/

this means I want only origins that have both “example.com” and “example.app” to work, according to so express documentation, so I parse the env variable as follows

export const CORS_ORIGINS = process.env.CORS_ORIGINS?.split(',')

but what is being set in the cors origins is ["/.example.com$/", "/.example.app$/"]
how do I make it this [/.example.com$/, /.example.app$/] instead.



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?