{"id":1027,"date":"2025-05-12T05:01:00","date_gmt":"2025-05-12T05:01:00","guid":{"rendered":"https:\/\/www.wordpress-prod.sharpener.tech\/?p=1027"},"modified":"2025-05-12T05:38:38","modified_gmt":"2025-05-12T05:38:38","slug":"script-for-reading-and-writing-from-file","status":"publish","type":"post","link":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/","title":{"rendered":"Script for reading and writing from file"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.wordpress-prod.sharpener.tech\/wp-content\/uploads\/2025\/05\/Script-for-reading-and-writing-from-file-1024x683.jpg\" alt=\"\" class=\"wp-image-1031\" srcset=\"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file-1024x683.jpg 1024w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file-300x200.jpg 300w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file-768x512.jpg 768w, https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">hello Everyone! I hope you are doing well. My name is Julian, and today we will learn how to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build a simple server that displays a form to collect data from users.<\/li>\n\n\n\n<li>An efficient way of managing and saving the collected data into a file.<br>A lot of things might sound new to you<br>By the end of this session,<br>you will clearly understand how to handle data efficiently in Node.js.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Part 1: Understanding Streams and Buffers<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before we start coding, let&#8217;s first understand two important concepts for data handling in a server: <strong>streams<\/strong> and <strong>buffers<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Stream:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A stream is like a pipeline that transports data in small chunks,<br>just like water flowing through a pipe.<br>Instead of sending all the data at once,<br>it sends it piece by piece.<br>This is especially helpful for handling large amounts of data,<br>such as when users fill out forms or upload files,<br>because it uses less memory and lets us start working with the data immediately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><strong><br><\/strong>Think of watching a movie online.<br>Instead of downloading the entire movie file before you can watch it,<br>the video player streams the movie.<br>It starts playing as soon as enough data is loaded,<br>allowing you to enjoy the movie without waiting for the whole file to download.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Buffer:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>buffer<\/strong> is a temporary storage area that holds small chunks of data as it flows through the stream.<br>Imagine filling a small bucket with water.<br>The bucket fills up, you pour it out and then fill it again.<br>This is how buffers work: they store data temporarily before passing it along.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Part 2: Setting Up a Simple Server<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s build a server that will display a form,<br>collect user input,<br>and handle it using streams and buffers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 1: Display the Form<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the user visits our server,<br>we\u2019ll display a simple form where they can input their name.<br>The form will look something like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">html<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Copy code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"\/submit\" method=\"post\"&gt;\n\n&nbsp;&nbsp;&lt;label&gt;Name:&lt;\/label&gt;\n\n&nbsp;&nbsp;&lt;input type=\"text\" name=\"name\"&gt;&lt;br&gt;&lt;br&gt;\n\n&nbsp;&nbsp;&lt;label&gt;Age:&lt;\/label&gt;\n\n&nbsp;&nbsp;&lt;input type=\"text\" name=\"age\"&gt;&lt;br&gt;&lt;br&gt;\n\n&nbsp;&nbsp;&lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n\n&lt;\/form&gt;\n\nLet\u2019s add this form to our server code.\n\nconst http = require('http');\n\nconst fs = require('fs');&nbsp; \/\/ We\u2019ll use fs to write data to a file later\n\nconst server = http.createServer((req, res) =&gt; {\n\n&nbsp;&nbsp;if (req.method === 'GET') {\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Show the form to the user\n\n&nbsp;&nbsp;&nbsp;&nbsp;res.writeHead(200, { 'Content-Type': 'text\/html' });\n\n&nbsp;&nbsp;&nbsp;&nbsp;res.end(`\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=\"\/submit\" method=\"post\"&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&gt;Name:&lt;\/label&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=\"text\" name=\"name\"&gt;&lt;br&gt;&lt;br&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&gt;Age:&lt;\/label&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=\"text\" name=\"age\"&gt;&lt;br&gt;&lt;br&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/form&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;`);\n\n&nbsp;&nbsp;}\n\n});\n\nserver.listen(3000, () =&gt; {\n\n&nbsp;&nbsp;console.log('Server running at http:\/\/localhost:3000');\n\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create a simple HTTP server using Node.js.<\/li>\n\n\n\n<li>When the user visits the server with a <strong>GET request<\/strong>, we show the form using res.end().<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Part 3: Handling Form Submission with Streams and Buffers<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the user submits the form, we need to collect the data. We\u2019ll use <strong>streams<\/strong> to handle the form data in small pieces and <strong>buffers<\/strong> to temporarily hold the incoming data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2: Collecting Data from the Form<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">After the form is submitted, the data will be sent to the server in small chunks. We can capture this data by using event listeners: req.on(&#8216;data&#8217;) and req.on(&#8216;end&#8217;).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s add that part to our code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>else if (req.method === 'POST' &amp;&amp; req.url === '\/submit') {\n\n&nbsp;&nbsp;const body = &#091;]; \/\/ Array to hold the chunks of data\n\n&nbsp;&nbsp;\/\/ Listen for the 'data' event to collect the form data in chunks\n\n&nbsp;&nbsp;req.on('data', chunk =&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;body.push(chunk); \/\/ Add the chunk to the body array\n\n&nbsp;&nbsp;});\n\n&nbsp;&nbsp;\/\/ Listen for the 'end' event, which tells us all the data has been received\n\n&nbsp;&nbsp;req.on('end', () =&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Concatenate all the chunks into a single buffer\n\n&nbsp;&nbsp;&nbsp;&nbsp;const buffer = Buffer.concat(body);\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Convert the buffer to a string\n\n&nbsp;&nbsp;&nbsp;&nbsp;const formData = buffer.toString();\n\n&nbsp;&nbsp;&nbsp;&nbsp;console.log('Data received:', formData);\n\n&nbsp;&nbsp;});\n\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>req.on(&#8216;data&#8217;): This event fires whenever a chunk of data is received. We add that chunk to our body variable.<\/li>\n\n\n\n<li>req.on(&#8216;end&#8217;): This event fires when all the chunks have been received. At this point, we can log or process the full form data.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Part 4: Saving Data to a File<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have collected the data, let\u2019s save it to a file using the <strong>fs<\/strong> module.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 3: Writing Data to a File<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019ll use Node\u2019s fs.writeFile() function to save the form data to a file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">javascript<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Copy code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>req.on('end', () =&gt; {\n\n&nbsp;&nbsp;const buffer = Buffer.concat(body);\n\n&nbsp;&nbsp;const formData = buffer.toString();\n\n&nbsp;&nbsp;console.log('Data received:', formData);\n\n&nbsp;&nbsp;\/\/ Save the form data to a file\n\n&nbsp;&nbsp;fs.writeFile('formdata.txt', formData, (err) =&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;if (err) throw err;\n\n&nbsp;&nbsp;&nbsp;&nbsp;console.log('Form data saved!');\n\n&nbsp;&nbsp;});\n\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>fs.writeFile(): This method writes data to a file. If the file doesn\u2019t exist, it will be created.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Part 5: Redirecting the User to a Success Page<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once the data is saved, we want to redirect the user to a success page. We\u2019ll use a <strong>302 redirect<\/strong> to send the user to a new URL.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 4: Redirecting the User<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">After saving the data, we\u2019ll redirect the user to a success page using the following code:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">javascript<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Copy code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>res.writeHead(302, { 'Location': '\/success' });\n\nres.end();\n\nNow let\u2019s create the success page:\n\njavascript\n\nCopy code\n\nelse if (req.url === '\/success') {\n\n&nbsp;&nbsp;\/\/ Show the success message\n\n&nbsp;&nbsp;res.writeHead(200, { 'Content-Type': 'text\/html' });\n\n&nbsp;&nbsp;res.end('&lt;h1&gt;Form Submitted Successfully!&lt;\/h1&gt;');\n\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>302 Redirect<\/strong>: This tells the browser to go to a different page (\/success).<\/li>\n\n\n\n<li>After the redirection, the user will see a success message.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Full Code Recap:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the complete server code with all the steps included:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">javascript<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Copy code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const http = require('http');\nconst fs = require('fs');\n\nconst server = http.createServer((req, res) =&gt; {\n  if (req.method === 'GET') {\n    \/\/ Display the form\n    res.writeHead(200, { 'Content-Type': 'text\/html' });\n    res.end(`\n      &lt;form action=\"\/submit\" method=\"post\"&gt;\n        &lt;label&gt;Name:&lt;\/label&gt;\n        &lt;input type=\"text\" name=\"name\"&gt;&lt;br&gt;&lt;br&gt;\n        &lt;label&gt;Age:&lt;\/label&gt;\n        &lt;input type=\"text\" name=\"age\"&gt;&lt;br&gt;&lt;br&gt;\n        &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n      &lt;\/form&gt;\n    `);\n  } else if (req.method === 'POST' &amp;&amp; req.url === '\/submit') {\n    const body = &#091;]; \/\/ Array to hold the chunks of data\n\n    \/\/ Listen for the 'data' event to collect the form data in chunks\n    req.on('data', chunk =&gt; {\n      body.push(chunk); \/\/ Add the chunk to the body array\n    });\n\n    \/\/ Listen for the 'end' event, which tells us all the data has been received\n    req.on('end', () =&gt; {\n      \/\/ Concatenate all the chunks into a single buffer\n      const buffer = Buffer.concat(body);\n      \n      \/\/ Convert the buffer to a string\n      const formData = buffer.toString();\n      console.log('Data received:', formData);\n\n      \/\/ Save the form data to a file\n      fs.writeFile('formdata.txt', formData, (err) =&gt; {\n        if (err) throw err;\n        console.log('Form data saved!');\n      });\n\n      \/\/ Redirect to success page\n      res.writeHead(302, { 'Location': '\/success' });\n      res.end();\n    });\n  } else if (req.url === '\/success') {\n    res.writeHead(200, { 'Content-Type': 'text\/html' });\n    res.end('&lt;h1&gt;Form Submitted Successfully!&lt;\/h1&gt;');\n  } else {\n    res.writeHead(404);\n    res.end('Page Not Found');\n  }\n});\n\nserver.listen(3000, () =&gt; {\n  console.log('Server running at http:\/\/localhost:3000');\n});\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Conclusion:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Streams<\/strong> help us handle data in chunks, which is useful for large inputs like forms.<\/li>\n\n\n\n<li><strong>Buffers<\/strong> temporarily store these chunks until all the data has been received.<\/li>\n\n\n\n<li>With <strong>event listeners<\/strong> like req.on(&#8216;data&#8217;) and req.on(&#8216;end&#8217;), we can manage form data efficiently.<\/li>\n\n\n\n<li>We also learned how to redirect the user and save form data to a file.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can handle form submissions and data efficiently in your Node.js servers! Thank you for joining the lecture, and happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: hello Everyone! I hope you are doing well. My name is Julian, and today we will learn how to: Part 1: Understanding Streams and Buffers Before we start coding,&hellip;<\/p>\n","protected":false},"author":6,"featured_media":1031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Read and Write Files in Node.js | File System (fs) Module Script Guide<\/title>\n<meta name=\"description\" content=\"Learn how to read and write files in Node.js using the built-in fs module. Includes examples of synchronous and asynchronous file operations with step-by-step script tutorials.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Read and Write Files in Node.js | File System (fs) Module Script Guide\" \/>\n<meta property=\"og:description\" content=\"Learn how to read and write files in Node.js using the built-in fs module. Includes examples of synchronous and asynchronous file operations with step-by-step script tutorials.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Sharpener Tech\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-12T05:01:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-12T05:38:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1536\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Julian Toppo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Julian Toppo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/\"},\"author\":{\"name\":\"Julian Toppo\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/person\\\/eb9482df421e52d30f961eae8a0fd67a\"},\"headline\":\"Script for reading and writing from file\",\"datePublished\":\"2025-05-12T05:01:00+00:00\",\"dateModified\":\"2025-05-12T05:38:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/\"},\"wordCount\":744,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/12050024\\\/Script-for-reading-and-writing-from-file.jpg\",\"articleSection\":[\"Tips\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/\",\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/\",\"name\":\"Read and Write Files in Node.js | File System (fs) Module Script Guide\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/12050024\\\/Script-for-reading-and-writing-from-file.jpg\",\"datePublished\":\"2025-05-12T05:01:00+00:00\",\"dateModified\":\"2025-05-12T05:38:38+00:00\",\"description\":\"Learn how to read and write files in Node.js using the built-in fs module. Includes examples of synchronous and asynchronous file operations with step-by-step script tutorials.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#primaryimage\",\"url\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/12050024\\\/Script-for-reading-and-writing-from-file.jpg\",\"contentUrl\":\"https:\\\/\\\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/12050024\\\/Script-for-reading-and-writing-from-file.jpg\",\"width\":1536,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/script-for-reading-and-writing-from-file\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Script for reading and writing from file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/\",\"name\":\"Sharpener Tech\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#organization\",\"name\":\"Sharpener Tech\",\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/wordpress-prod.sharpener.tech\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Sharpener_logo-removebg-preview.png\",\"contentUrl\":\"https:\\\/\\\/wordpress-prod.sharpener.tech\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Sharpener_logo-removebg-preview.png\",\"width\":187,\"height\":62,\"caption\":\"Sharpener Tech\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/#\\\/schema\\\/person\\\/eb9482df421e52d30f961eae8a0fd67a\",\"name\":\"Julian Toppo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/87799fed7134aa3bb27606b4eceeb577117023517d38b0152340c9e59376052c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/87799fed7134aa3bb27606b4eceeb577117023517d38b0152340c9e59376052c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/87799fed7134aa3bb27606b4eceeb577117023517d38b0152340c9e59376052c?s=96&d=mm&r=g\",\"caption\":\"Julian Toppo\"},\"url\":\"https:\\\/\\\/www.sharpener.tech\\\/blog\\\/author\\\/julian\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Read and Write Files in Node.js | File System (fs) Module Script Guide","description":"Learn how to read and write files in Node.js using the built-in fs module. Includes examples of synchronous and asynchronous file operations with step-by-step script tutorials.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/","og_locale":"en_US","og_type":"article","og_title":"Read and Write Files in Node.js | File System (fs) Module Script Guide","og_description":"Learn how to read and write files in Node.js using the built-in fs module. Includes examples of synchronous and asynchronous file operations with step-by-step script tutorials.","og_url":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/","og_site_name":"Sharpener Tech","article_published_time":"2025-05-12T05:01:00+00:00","article_modified_time":"2025-05-12T05:38:38+00:00","og_image":[{"width":1536,"height":1024,"url":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg","type":"image\/jpeg"}],"author":"Julian Toppo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Julian Toppo","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#article","isPartOf":{"@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/"},"author":{"name":"Julian Toppo","@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/person\/eb9482df421e52d30f961eae8a0fd67a"},"headline":"Script for reading and writing from file","datePublished":"2025-05-12T05:01:00+00:00","dateModified":"2025-05-12T05:38:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/"},"wordCount":744,"commentCount":0,"publisher":{"@id":"https:\/\/www.sharpener.tech\/blog\/#organization"},"image":{"@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#primaryimage"},"thumbnailUrl":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg","articleSection":["Tips"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/","url":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/","name":"Read and Write Files in Node.js | File System (fs) Module Script Guide","isPartOf":{"@id":"https:\/\/www.sharpener.tech\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#primaryimage"},"image":{"@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#primaryimage"},"thumbnailUrl":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg","datePublished":"2025-05-12T05:01:00+00:00","dateModified":"2025-05-12T05:38:38+00:00","description":"Learn how to read and write files in Node.js using the built-in fs module. Includes examples of synchronous and asynchronous file operations with step-by-step script tutorials.","breadcrumb":{"@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#primaryimage","url":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg","contentUrl":"https:\/\/sharpener-wordpress.s3.ap-south-1.amazonaws.com\/blog\/wp-content\/uploads\/2025\/05\/12050024\/Script-for-reading-and-writing-from-file.jpg","width":1536,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/www.sharpener.tech\/blog\/script-for-reading-and-writing-from-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sharpener.tech\/blog\/"},{"@type":"ListItem","position":2,"name":"Script for reading and writing from file"}]},{"@type":"WebSite","@id":"https:\/\/www.sharpener.tech\/blog\/#website","url":"https:\/\/www.sharpener.tech\/blog\/","name":"Sharpener Tech","description":"","publisher":{"@id":"https:\/\/www.sharpener.tech\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sharpener.tech\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.sharpener.tech\/blog\/#organization","name":"Sharpener Tech","url":"https:\/\/www.sharpener.tech\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/wordpress-prod.sharpener.tech\/wp-content\/uploads\/2026\/05\/Sharpener_logo-removebg-preview.png","contentUrl":"https:\/\/wordpress-prod.sharpener.tech\/wp-content\/uploads\/2026\/05\/Sharpener_logo-removebg-preview.png","width":187,"height":62,"caption":"Sharpener Tech"},"image":{"@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.sharpener.tech\/blog\/#\/schema\/person\/eb9482df421e52d30f961eae8a0fd67a","name":"Julian Toppo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/87799fed7134aa3bb27606b4eceeb577117023517d38b0152340c9e59376052c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/87799fed7134aa3bb27606b4eceeb577117023517d38b0152340c9e59376052c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/87799fed7134aa3bb27606b4eceeb577117023517d38b0152340c9e59376052c?s=96&d=mm&r=g","caption":"Julian Toppo"},"url":"https:\/\/www.sharpener.tech\/blog\/author\/julian\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/comments?post=1027"}],"version-history":[{"count":4,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"predecessor-version":[{"id":1032,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/posts\/1027\/revisions\/1032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/media\/1031"}],"wp:attachment":[{"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sharpener.tech\/blog\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}