Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1662

Get path to read file from /static (SvelteKit)

$
0
0

I have a file "artists.csv" which i need to read during execution of my server side code (when an API request is made, to be specific).

new Promise((resolve, reject) => {    fs.createReadStream(artistsFilePath)        ...});

I have made the assumption the static folder is the correct place to put such a file. So now I need the path for the file in the static folder. In the html frontend %sveltekit.assets%/favicon.png is used but this does not work in my case. The only way I found to make it work in dev as well as prod is this piece of spaghetti:

const __dirname = dirname(fileURLToPath(import.meta.url));let artistsFilePath = __dirname +'/../../../../../client/artists.csv';if (!fs.existsSync(artistsFilePath)) {    // Development mode    artistsFilePath = __dirname +'/../../static/artists.csv';}

Surely there is a better way to do this?


Viewing all articles
Browse latest Browse all 1662

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>