Home > Net >  read/import file from docker volume in Angular-Typescript
read/import file from docker volume in Angular-Typescript

Time:04-27

I have 2 containers, one for Angular and another for NodeJS. In NodeJS, I write data to a file and store it in Docker Shared volume.

This is how I access file in NodeJS from Shared volume:

const dataPath = path.join(process.env.DATA_PATH || "./data.txt")

Below is my docker run command for NodeJS container:

docker run -d --env DATA_PATH=/data/data.txt --mount type=volume,src=file-st,target=/data  -p 3001:3001  <IMAGE TAG>

This works fine and the data is written successfully to shared volume. But the problem I am facing is that I am not able to understand how to access "/data" shared volume data in Angular. Is there anything equivalent to process.env.DATA_PATH of NodJS in Angular?

Below import line in Angular throws compilation error:

import sampleData from './data/data.txt'

I am pretty sure what I am doing above is incorrect. I just need some guidance on this.

CodePudding user response:

You won't be able to access a file this way after your Angular app is compiled, as files imported this way are only dealt with during compilation.

In short, you are dealing with dynamic content here that should be loaded as such by your Angular app via a GET request with the HttpClient. Generally, in a Client-Server architecture like this, you would serve the text file on the server (NodeJS), and retrieve it in the client (Angular).

  • Related