Node.js Tutorial --Reading and Writing files

in GEMS4 years ago

Hello Everyone

<p dir="auto">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. <ul> <li><p dir="auto">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. <li><p dir="auto">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. <li><p dir="auto">Open the VS code/Subline ... <li><p dir="auto">open the folder where you want to keep the files. If you have Git installed bash on the folder and type <code>touch index.js to create a new .js file. If you don't have git just create a file and name it .js. <li><p dir="auto">now include the fs module. <p dir="auto"><img src="https://images.hive.blog/DQmRWiCtHc6SWoTepbNcshygMEafD9Df6RC4Czn59TL5foj/image.png" alt="image.png" /> <p dir="auto">Let's say I want to create a .txt file and store some text in that file. <p dir="auto"><img src="https://images.hive.blog/DQmNiUcysXEWNDawz3NfA5WxfeXqHMNDznKb5Bs1BokE63e/image.png" alt="image.png" /> <p dir="auto">Open a command prompt or git to run the script. <p dir="auto"><img src="https://images.hive.blog/DQmYqahd2Pai6fSme5N5QGy6eyoSnBwB1ovME8fsTxjXMxi/image.png" alt="image.png" /> <p dir="auto">This will not print anything console because it is sync and does not contain a call back function. <p dir="auto"><img src="https://images.hive.blog/DQmcAWZ116BnLvwVmFSiHo6hh1fqogx8qEK53yKEWfpWULD/image.png" alt="image.png" /><br /> We have created the file and stored the text in it. ( The file wasn't there before); <ul> <li>Now let's access the same file through fs. We will read the same file using the fs.readFile. <p dir="auto"><img src="https://images.hive.blog/DQmRJTWeh6wZR435NeKetDhofx1unSxgLn12xKCE62azSy6/image.png" alt="image.png" /> <p dir="auto">This time we are storing the text from the file in a variable and then show the text on the console. <p dir="auto"><img src="https://images.hive.blog/DQmVdNDpFYZb334wbnH297nBU5rEgqAqXUE29LbRBhNUh8A/image.png" alt="image.png" /> <p dir="auto">Now let's use a call back function while writing a file because we are getting any error or success message while executing. <p dir="auto"><img src="https://images.hive.blog/DQmVWpw5EmQ6LagNVW77wVZKhZA6ucGDTvvCfNDeNApzvCh/image.png" alt="image.png" /> <p dir="auto">Now If the code able to create and edit file it will display a message <code>done editing else if it catch some error it will throw that error. <p dir="auto"><img src="https://images.hive.blog/DQmTsWyNrQ4MA3W9uZW7s8JiQpyJR3zpfo9KdD5Yqafx3Fo/image.png" alt="image.png" /> <p dir="auto"><center><br /> <strong>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<br /> <img src="https://images.hive.blog/DQmd3WcHH8i6U2JkmnrvEvd4FwjDQxJtG3Tnhzxdjswktdx/ntitled-1.png" alt="ntitled-1.png" />
Sort:  

Congratulations @pakgamer! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@pakgamer/upvoted.png?202007191359" /><td>You received more than 3250 upvotes. Your next target is to reach 3500 upvotes. <p dir="auto"><sub><em>You can view <a href="https://hivebuzz.me/@pakgamer" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your badges on your board And compare to others on the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto">To support your work, I also upvoted your post! <p dir="auto"><strong><span>Do not miss the last post from <a href="/@hivebuzz">@hivebuzz: <table><tr><td><a href="/hivebuzz/@hivebuzz/hivewhale"><img src="https://images.hive.blog/64x128/https://i.imgur.com/rEHA937.png" /><td><a href="/hivebuzz/@hivebuzz/hivewhale">Hive Whale - Make it spray and get your badge! <h6>Support the HiveBuzz project. <a href="https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22109%22%5D&approve=true" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for <a href="https://peakd.com/me/proposals/109" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">our proposal!