Node.js Tutorial --Reading and Writing files
Hello Everyone
Today in this post we are going to learn a node module that will help us read and write a file through a simple script. This Module will help us in the upcoming tutorials because we are going to create some useful Hive tools. So without wasting time let's begin.
First of all, we need the Node.js installed. If you don't have Node.js installed. Install it by visiting its official page to download the package according to your operating system. As I already have Node.js installed so I will skip this step.
Another piece of software you need is a plain text editor. I am using a Subline text editor. You can use other editors like subline, Atom, etc.
Open the VS code/Subline ...
open the folder where you want to keep the files. If you have Git installed bash on the folder and type
touch index.jsto create a new .js file. If you don't have git just create a file and name it .js.now include the fs module.
Let's say I want to create a .txt file and store some text in that file.
Open a command prompt or git to run the script.
This will not print anything console because it is sync and does not contain a call back function.
We have created the file and stored the text in it. ( The file wasn't there before);
- Now let's access the same file through fs. We will read the same file using the fs.readFile.
This time we are storing the text from the file in a variable and then show the text on the console.
Now let's use a call back function while writing a file because we are getting any error or success message while executing.
Now If the code able to create and edit file it will display a message done editing else if it catch some error it will throw that error.
I think this much is enough for today. Now if you like the post please upvote and comment if you want to give me some advise
Leave Node.js Tutorial --Reading and Writing files to:
Read more #nodejs posts
Best Posts From ProGrammer
We have not curated any of pakgamer's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From ProGrammer
- What do you think your Password is Secure?
- Nodejs Tutorial-- Sending Email using NodeMailer
- Node.js Tutorial --Reading and Writing files
- How to install Nodemon-- Express.js Tutorials
- How to use Range Element in a simple voting tool
- Learning Express.js-- installation and Hello World Example
- How to Compile your C/C++ code on the Visual Studio Code.
- How to check Voting Power And Resource Credit of Hive Account Through dhive
- How To Send Message TO WhatsApp Using JavaScript/Node.js-- Twilio
- How to add Custom prefix to your Discord Bot. Discordjs Tutorial
- HiveJs Tutorials --Learning how to transfer HIVE/HBD thorugh HiveJs
- Fetching API -- JavaScript Tutorial
- Learning Local Storage in JavaScript..
- Hivejs Tutorials-- #3
- Learn jQuery. Learning Events/Methods
- Learn jQuery --- Learning Selectors
- Learning JQuery-- Understanding the basics
- TypeScript for beginners -- A quick start
- How to Stream Hive Blockchain Transactions.-- HIVED
- Grand Theft Auto V (GTA 5) is now free on Epic Games Store.