Sunday 21 May 2017

Parse a string to array in node.js

I am trying to wrap my Node.js app with Docker. One of the things I need to get from the image runner is an environment variable that contains an array.

For example:

docker run -d -e DM_HOSTS=['localhost:1234', 'localhost'] someimage...

And my code:

let hosts:string[] = process.env.DM_HOSTS ? process.env.DM_HOSTS : ['localhost:1234'];

The problem is - I need to parse this environment variable to a real JS array.

I tried JSON.parse() and Array.from() and it didn't work.

Appriciate your help!



via user3328621

No comments:

Post a Comment