{"id":4419,"date":"2019-05-02T11:23:16","date_gmt":"2019-05-02T05:53:16","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4419"},"modified":"2019-05-02T21:33:01","modified_gmt":"2019-05-02T16:03:01","slug":"node-js-file-system-module","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/node-js-file-system-module\/","title":{"rendered":"NODE JS FILE SYSTEM MODULE"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>NODE JS FILE SYSTEM MODULE<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_0\"><strong>Date posted :02\/05\/2019<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"> In this blog we are going to see about nodejs file modules and how to read create, update and delete the files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>INTRODUCTION:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">     Nodejs file system used to work with the file system on your computer.<br>     File system module use the require() methods.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>FILE SYSTEM MODULES\ufeff<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>     Read the file<\/li><li>     create the file<\/li><li>     update the file<\/li><li>     delete the file<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;html style=\"background: deepskyblue;\">\n&lt;body>\n&lt;h1>Welcome to Nodejs&lt;\/h1>\n&lt;p>This is my first file operation&lt;\/p>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create the html file for read the data. And save it as test.html<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Read Files:<\/strong>                 <\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1)<strong>fs.readFile()<\/strong>    <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">                This function used to read the file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var http = require('http');\nvar fs = require('fs');\nhttp.createServer(function (req, res) {\n  fs.readFile('test.html', function(err, data) {\n    res.writeHead(200, {'Content-Type': 'text\/html'});\n    res.write(data);\n    res.end();\n  });\n}).listen(8080);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">                 Create the nodejs file and save it as testread.js. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    Then run the following code in to your terminal. You can get the following out put.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>> node testread.js\ufeff<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"306\" height=\"154\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2019-05-02-at-10.57.47-AM.png\" alt=\"\" class=\"wp-image-4425\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2019-05-02-at-10.57.47-AM.png 306w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Screenshot-2019-05-02-at-10.57.47-AM-300x151.png 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>CREATE A FILE:\ufeff<\/strong><br><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1)fs.appendFile()<\/strong><br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">        This method appends specified content  to the end of the file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fs = require('fs');\nfs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {\n   if (err) throw err;\n   console.log('Saved!');\n });<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2)fs.open()<\/strong><br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">         This method takes a &#8220;flag&#8221; as the second arguments,w for write,the file is opened for writing.<br>         If not exist empty file will create.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fs = require('fs');\n\nfs.open('mynewfile2.txt', 'w', function (err, file) {\n  if (err) throw err;\n  console.log('Saved!');\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3)fs.writeFile()\ufeff<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">     The above method create the new file file1 and save the content &#8220;Hello content&#8221;.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fs = require('fs');\n\nfs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {\n  if (err) throw err;\n  console.log('Saved!');\n});<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>UPDATE FILES:\ufeff<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">    1)fs.appendFile()<br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">           This method append the text end of the file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fs = require('fs');\n\nfs.appendFile('test', ' This is my text.', function (err) {\n  if (err) throw err;\n  console.log('Updated!');\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2)fs.writeFile()<\/strong><br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This method used to replace the content of the file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fs = require('fs');\n\nfs.writeFile('mynewfile3.txt', 'This is my text', function (err) {\n  if (err) throw err;\n  console.log('Replaced!');\n});<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>DELETE FILE:<\/strong><\/h3>\n\n\n\n<h2 class=\"wp-block-heading\">   <strong>1)fs.unlink()<\/strong><br><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This method used to delete the file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fs = require('fs');\n\nfs.unlink('filename.txt', function (err) {\n  if (err) throw err;\n  console.log('File deleted!');\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> Thanks for using <strong>pheonixsolutions<\/strong>.   <br><br>You find this tutorial helpful? Share with your friends to keep it alive.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>NODE JS FILE SYSTEM MODULE Date posted :02\/05\/2019 In this blog we are going to see about nodejs file modules [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[644,506,642,643,337],"class_list":["post-4419","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-append","tag-delete","tag-fs-open","tag-fs-write","tag-nodejs","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-19h","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4419"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4419\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}