Home > Enterprise >  Using txt-files or SQL database
Using txt-files or SQL database

Time:12-09

I pretty new to web-development and i would like to make a webpage that list song texts. My thought were to put each song text in a separate txt-files in a folder and then use javascript to go through all txt-files, and then display the content. However, I have understod that it is not so easy to read data from files using javascript, is that correct? That would be a prefered method?

The thought of using txt-file was the easiness to write new songs and remove songs just uploading or deleting the txt-files.

I have search for a solution to go find all text files using javascript.

CodePudding user response:

If you don't plan on having hundreds of songs, the easiest way for a beginner would probably be to have one (or more) data.js file in which you store your data as an array of objects. Something like this:

const songs = [
{title: "song1", text:"..."}
{title: "song2", text:"..."}
];

With a lot of songs it would soon become very messy but a least you can very easily import that data in your script and it would be ready to use. So, easy to start with but not very maintainable long term.

I would not recommend using text files.

Now the best way would obviously to use a real database, such as MongoDB Atlas for example (or an SQL one), but it involves having to build a backend to interact with this DB so a lot more work to do for you, so it also depends on how much time and effort you want to put into this.

CodePudding user response:

Either way you would have to create a back-end server since javascript cannot read txt files from local machine due to security reasons.

If you choose to have your own database system using .txt files you would have to implement all of the methods for getting and setting items in database by yourself which would be a huge job. This way you would have more control over your database but this is a much bigger job than working with an already existing database. If you are a beginner I suggest MongoDB for your database, since it's easy to work with and you can just add a json file containing your song lyrics, song name, release date, etc.. This is really similar to working with text files and even gives you more flexibility on adding song characteristics such as genres and so on.

Example of Song .json file containing a song in MongoDB:

"Song": {
  "name": "You give love a bad name",
  "artist": "Bon Jovi",
  "release_year": "1986"
}

For either case you would have to setup an express back-end server that will handle the request from front-end, extract the requested song from the database and return a response object.

CodePudding user response:

If you have your song texts on a server you can fetch the text file with javascript like this:

fetch('https://some.domain/file.txt')
  .then(response => response.text())
  .then(text => {
     // do here something with the textfile
     console.log(text)
  })

If your text files are on the local machine, you can use the File System API, but for the time being it does not work in firefox:

https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API

  • Related