Is it possible to read and write a file csv-file with a locally deployed NextJS app?
The server will run on my system and is just a frontend for some work I would like to do on the data in the csv. After that, just want to save the data back to the original file. I thought doing it with a browser frontend would be the fastest way to deploy a functional solution, with out the learning overhead of something like Qt, or the torture of using Excel for it.
How can I read/write from a csv-file, for example in the public folder, on start-up of the app, or on a button click for example?
Is the theoretically possible, or am I getting things mixed up? My experience with React/NextJS is very limited.
2
Answers
</s>
I would like to thank everyone who downvoted but never left a comment explaining why for their constructive criticism and continued effort to make Stack Overflow a better website and online resource.</s>
So, to answer my own question, I tried to use ./src/app/api with certain routes and handlers to solve my problem, but unbeknownst to me, since Next.js v14 (I think), the correct way is:
/src/app/api//route.ts
In this file, define each of the CRUD methods separately, for example, export async function GET(...), etc.
In those API requests, the server-side of the Next.js application has full access to the file system, and I was able to read and write to a CSV file in the application folder.
I hope this helps. It took me three days to realize that the version change affected my API routes.
What you want to achieve is possible but not that straightforward, where the challenge lies is modifying the csv file as browsers cannot write to a user file system. Here is what you can do:
Reading and parsing the csv file – You can use packages like papaparse and react-csv-reader to read and parse your csv file.
Modifying and writing to a csv isn not directly possible because of the concern I mentioned earlier, so what you will do instead is to create a new csv file with the modified content of the first csv file.
Hope this helps.