Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Hello, fellow members of the nightās watch. We will be looking at one of Nodeās core APIās, the File System. I know weāve been concerned with code splitting, bundle splitting, preloading and the never ending woes of the Internet Explorer, but we will be learning how Node interacts with our files and directories. This helps us handle images or assets on our server, store them properly, read the content of other files, write to files,etc.
We will be building a basic CRUD application using the file system. I think we can getĀ started.
Our little folder structure should look likeĀ this:
Database-----(folder)crud.js------(file)
The āDatabaseā folder is going to contain a file, that will hold our Automobile business data. This data is going to comprise of the name of a car, and itās price. It will be stored in JSON. Obviously, this cannot be done in production, and in order to ensure that no one thinks of it, iām stating it unequivocally.
CREATE
Now, we will be creating our cars.json file, from our crud.js file. Firstly, we will need the file system module, and the pathĀ module.
const fs = require('fs');const path = require('path');
Then, we will create the crud object and its base directory, which holds the path from the crud.js file to the databaseĀ folder.
const crud = {};crud.baseDir = path.join(__dirname,'./database');
Now, we will add a create function, and our crud.js file should look likeĀ this:
The ācreateā function, holds the name of the file we want to create, and the data we want to write to the file. So we open a new file in the path we defined in the base directory, and it takes whatever name weĀ assign.
āwxā means we can write to the file, but the operation will fail if the file already exists. There are so many other operations we can attribute to theĀ file.
If there are no errors, Node attributes an identifier or a file descriptor to the file. If this identifier is found, we can then write to theĀ file.
We want our database to look likeĀ this:
[ {name: '', price: ''}, {name: '', price: ''}]
This is the reason, we push the data to the jsonArray variable, before sending it to theĀ file.
We can go to the bottom of our page, and runĀ this:
crud.create('cars',{'name': 'innoson','price':'$4000'});
//Then Obviously(in your console)node crud
All things being equal, this should create a cars.json file with the array insideĀ it.
READ
Now, we should be able to read the data from the file we just created. Our read function should look likeĀ this:
This function goes to the base directory, which is the database folder and then reads the data from the file we pass into the function. The encoding format is UTF-8, and without errors, our data should getĀ logged.
We can go to the bottom of our page and runĀ this:
crud.read('cars');
UPDATE
Now, we can use fs.truncate() to change the content of the whole file, but we will use fs.appendFile() to append data to ourĀ file.
If we go to the bottom of the page, and run the updateĀ function
crud.update('cars',{name:'Tesla',price:'$10'})
We will have an output likeĀ this:
[ {name: 'innoson',price:'#4000'}],{name: 'Tesla',price:'$10'}
which is not what we desire, we need the object to be in the array. To do this, i came up with aĀ plan.
- We will read the current content of the cars.json file
- Then, we will append ourĀ updates
- Then, we will truncate the file andĀ replace.
With the emphasis on āThenā, we will need to use promises. We will make use of another Node API,Ā util.
Go to the top of your crud.js file andĀ include:
const { promisify } = require("util");
const readFile = promisify(fs.readFile);
So, the āreadFileā variable will return promises. Our new crud.update function should look likeĀ this:
So, we can create a new JSON file in the database folder, and test our update function.
crud.create('cars-updated',{name:'mercedes',price:'$400'})
crud.update('cars-updated',{name:'Toyota',price:'$550'})
The cars-updated.json file should have the desiredĀ format.
DELETE
To delete a file, we use the fs.unlink method. So our crud.delete function should look likeĀ this:
So, we can delete the cars.json file, since its no longerĀ needed.
crud.delete('cars')
You can see the crud.js fileĀ here
Thank you very much for reading,if you liked it, please clap. if you have any questions or you need further clarification, you can leave a comment or a mail(paschalobba@yahoo.com).
Workaround the Node JS File System was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.