-2

Say I have a website hosted via Bluehost or the like, at example.com. If I have a file stored at example.com/a.txt, how can I go about reading from this file? And, if I want to write a new string to this file, is it possible to do so, or is it impossible with JavaScript in the web?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
kmindspark
  • 487
  • 1
  • 7
  • 18
  • 1
    You read it using ajax (XHR, fetch, whatever). You can only write to it with the help of server-side code. – Heretic Monkey Oct 23 '20 at 19:26
  • 1
    Does this answer your question? [How do I load the contents of a text file into a javascript variable?](https://stackoverflow.com/questions/196498/how-do-i-load-the-contents-of-a-text-file-into-a-javascript-variable) – Zak Oct 23 '20 at 19:26

2 Answers2

1

you could make a simple api using express and use file-system or fs with that package you can do that with a single line after doing const fs = require("fs"); :

const constant = fs.readFileSync("path");  //read the file
fs.writeFile("path", "something to write", () => {
    //a callback func
  });```
1

In short no. But you have workarounds:

You can open some folder for the public view:

static files - read only access (styles and javascript)
media files - read only access (pictures and other media)
some manually created url - to edit/create the file in the media folder
Yuriy Gyerts
  • 1,464
  • 18
  • 30