

{"id":100676,"date":"2021-10-04T09:00:06","date_gmt":"2021-10-04T03:30:06","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=100676"},"modified":"2021-10-04T12:39:21","modified_gmt":"2021-10-04T07:09:21","slug":"nodejs-modules","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/","title":{"rendered":"Nodejs Modules &#8211; Types and Examples"},"content":{"rendered":"<p>Modules are encapsulated code blocks that communicate with an external application. These can be a single file or a collection of multiple files\/folders. These are reusable, hence they are widely used. Let us learn about Nodejs Modules.<\/p>\n<h3>Types of Modules in Nodejs:<\/h3>\n<p>There are three types of modules<br \/>\n1) Core Modules<br \/>\n2) local Modules<br \/>\n3) Third-party Modules<\/p>\n<h4>1. Nodejs Core Modules:<\/h4>\n<p>Built-in modules of node.js that are part of nodejs and come with the Node.js installation process are known as core modules.<\/p>\n<p>To load\/include this module in our program, we use the <strong>require<\/strong> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let  module = require('module_name')\r\n<\/pre>\n<p>The return type of require() function depends on what the particular module returns.<\/p>\n<p>Http, file system and url modules are some of the core modules. We will be discussing this in the latter part of this blog.<\/p>\n<h4>2. Nodejs Local Modules:<\/h4>\n<p>Local modules are created by us locally in our Node.js application. These modules are included in our program in the same way as we include the built in module.<\/p>\n<p>Let&#8217;s build a module with the name as sum to add two numbers and include them in our index.js file to use them.<\/p>\n<p><strong>Code for creating local modules and exporting:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">exports.add=function(n,m){\r\n    return n+m;\r\n};\r\n<\/pre>\n<p>Exports keyword is used to make properties and methods available outside the file.<\/p>\n<p>In order to include the add function in our index.js file we use the require function.<\/p>\n<p><strong>Code for including local modules:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let sum = require('.\/sum')\r\n \r\nconsole.log(\"Sum of 10 and 20 is \", sum.add(10, 20))\r\n<\/pre>\n<p>Add the above code in a index.js file<\/p>\n<p>To run this file, open a terminal in the project directory and type node index.js and press enter. You can see the result of addition of 10 and 20. This addition has been performed by the add function in the sum module.<\/p>\n<p>&nbsp;<\/p>\n<h4>3. Nodejs Third Party Modules:<\/h4>\n<p>Modules that are available online and are installed using the npm are called third party modules. Examples of third party modules are express, mongoose, etc.<\/p>\n<p>To install third party modules refer to the previous blog where we have discussed how to install modules using npm.<\/p>\n<h3>Nodejs HTTP Module:<\/h3>\n<p>It is a built-in module of node.js. It allows node.js applications to transfer data using HyperText Transfer Protocol (HTTP).<\/p>\n<p>This module creates an HTTP server that listens to server ports and also gives responses back to the client.<\/p>\n<h4>Properties:<\/h4>\n<p><strong>1. http.METHODS:<\/strong> this tells us all the methods available in http module.<\/p>\n<p><strong>Code to check HTTP methods:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let http = require('http');\r\nconsole.log(http.METHODS)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100877\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods.png\" alt=\"nodejs http methods\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-methods-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><strong>2. http.STATUS_CODES:<\/strong> It tells us all the status codes and its description.<\/p>\n<p><strong>Code to check HTTP status codes:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let http = require('http');\r\nconsole.log(http.STATUS_CODES)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100878\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode.png\" alt=\"nodejs http status code\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-statuscode-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>METHODS:<\/h4>\n<p><strong>CREATE SERVER:<\/strong> We can use it to create a server. The create server will take a function which will run when we try to access the port.<\/p>\n<p><strong>listen()<\/strong> starts the HTTP server and listens for connections.<\/p>\n<p>Below code sets up a server which we can access when we visit localhost:3000<\/p>\n<p><strong>Code for creating http server:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let http = require('http');\r\nhttp.createServer(function (req, res) {\r\n    res.write('Welcome to DataFlair!');\r\n    res.end();\r\n}).listen(3000);\r\n<\/pre>\n<p>Now visit localhost:3000, you will see the message<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100879\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output.png\" alt=\"nodejs http output\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-http-output-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>Adding HTTP header:<\/h4>\n<p>If the message from the server needs to be shown as an HTML, then we have to include content type in the header.<\/p>\n<p><strong>Code for adding http header:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var http = require('http');\r\nhttp.createServer(function (req, res) {\r\n    res.writeHead(200, { 'Content-Type': 'text\/html' });\r\n    res.write('Hello from DataFlair!');\r\n    res.end();\r\n}).listen(3000);\r\n<\/pre>\n<h4>Reading query string:<\/h4>\n<p>The function in the create server has a request argument and this request object has a property of URL. It contains the part of the url that is present after the domain name.<\/p>\n<p>So when you go to localhost:3000\/dataflair\/nodejs, the output will be dataflair\/nodejs<\/p>\n<p><strong>Code for reading query string:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var http = require('http');\r\nhttp.createServer(function (req, res) {\r\n    res.writeHead(200, { 'Content-Type': 'text\/html' });\r\n    res.write(req.url);\r\n    res.end();\r\n}).listen(3000);\r\n<\/pre>\n<h3>Nodejs URL Module:<\/h3>\n<p>This is a built-in module of node.js. It breaks down the url into readable parts. It is included in the file by using the require function;<\/p>\n<p>To Parse an address, use the url.parse() method. It will return a URL object with each part of the address as its properties.<\/p>\n<p><strong>Code for using the url Module:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let url = require('url');\r\nvar adr = 'http:\/\/localhost:3000\/search?year=2021&amp;month=august';\r\nvar q = url.parse(adr, true);\r\n \r\nconsole.log(q.host);\r\nconsole.log(q.pathname);\r\nconsole.log(q.search);\r\n \r\nvar qdata = q.query;\r\nconsole.log(qdata.month);\r\n<\/pre>\n<p><strong>OUTPUT<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100881\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module.png\" alt=\"nodejs url module\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-url-module-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>In the above image you can see that the url has been parsed and individual parts are shown in the console.<\/p>\n<h4>File Server:<\/h4>\n<p>Now we will be parsing the url and based on it we will be returning the content of the requested file. If the file is not found we will get an 404 error message. So first create a html\/text file and add some content. We have made a text file with name as \u201cDataFlair.txt\u201d and it contains the line \u201cWelcome to DataFlair\u201d .<\/p>\n<p><strong>Code for serving a file over http:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var http = require('http');\r\nvar url = require('url');\r\nvar fs = require('fs');\r\n \r\nhttp.createServer(function (req, res) {\r\n    var q = url.parse(req.url, true);\r\n    var filename = \".\" + q.pathname;\r\n    fs.readFile(filename, function (err, data) {\r\n        if (err) {\r\n            res.writeHead(404, { 'Content-Type': 'text\/html' });\r\n            return res.end(\"404 Not Found\");\r\n        }\r\n        res.writeHead(200, { 'Content-Type': 'text\/html' });\r\n        res.write(data);\r\n        return res.end();\r\n    });\r\n}).listen(3000);\r\n<\/pre>\n<p>Now go to http:\/\/localhost:3000\/DataFlair.txt you will get the contents present in the file.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100882\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output.png\" alt=\"nodejs file server output\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-file-server-output-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>Nodejs File System Module:<\/h4>\n<p>This is a built-in module of node.js. It helps to create, read, write, update, or delete files in our computer. It is included in the file by using the require function.<\/p>\n<h4>Create File: It has mainly 3 methods<\/h4>\n<p><strong>1. Append file.<\/strong><\/p>\n<p>The fs.appendFile() method\u2019s second argument takes the text that we want to append, and then it appends at the end of the file, if the given file does not exist then the file will be created.<\/p>\n<p><strong>Code for appending text in a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs=require(\u2018fs\u2019)\r\nfs.appendFile('DataFlairDemo.txt', 'welcome to DataFlair', function (err) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<p><strong>OUTPUT<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100883\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file.png\" alt=\"nodejs fs append file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><strong>2. Open file<\/strong><\/p>\n<p>The fs.open() method\u2019s second argument is &#8220;w&#8221; for &#8220;writing&#8221;,which indicates that the file is opened for writing and if it does not exist then an empty file is created.<\/p>\n<p><strong>Code for opening a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs=require(\u2018fs\u2019)\r\nfs.open('DataFlairDemo.txt', 'w', function (err, file) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<p><strong>OUTPUT<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100884\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile.png\" alt=\"nodejs fs open file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-openfile-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that a file has been added.<\/p>\n<p><strong>3. WriteFile:<\/strong><\/p>\n<p>The fs.writeFile() method\u2019s second argument takes the text that we want to write, and then it overrides the content available in the file if the given file does not exist then the file will be created.<\/p>\n<p><strong>Code for writing in a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nfs.writeFile('DataFlairDemo.txt', 'Learn Node.js from DataFlair', function (err, file) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<p><strong>OUTPUT<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100885\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile.png\" alt=\"nodejs fs write file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>Update File: It has mainly 2 methods<\/h4>\n<p><strong>1) fs.appendFile()<\/strong><\/p>\n<p>The fs.appendFile() method\u2019s second argument takes the text that we want to append, and then it appends at the end of the file, if the given file does not exist then the file will be created.<\/p>\n<p><strong>Code for fs.appendFile()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs=require(\u2018fs\u2019)\r\nfs.appendFile('DataFlairDemo.txt', 'welcome to DataFlair', function (err) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100883\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file.png\" alt=\"nodejs fs append file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-append-file-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><strong>2) fs.writeFile():<\/strong><\/p>\n<p>The fs.writeFile() method\u2019s second argument takes the text that we want to write, and then it overrides the content available in the file, if the given file does not exist then the file will be created.<\/p>\n<p><strong>Code for writing in a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nfs.writeFile('DataFlairDemo.txt', 'Learn Node.js from DataFlair', function (err, file) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<p><strong>OUTPUT<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100885\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile.png\" alt=\"nodejs fs write file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-wrtefile-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>Read File:<\/h4>\n<p>It is used to read files present in your system.<\/p>\n<p><strong>Code for reading a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nfs.readFile('DataFlairDemo.txt', function (err, file) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<h4>Delete File: to delete a file use fs.unlink();<\/h4>\n<p><strong>Code for deleting a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nfs.unlink('DataFlairDemo.txt', function (err, file) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<p>You can see that the file is no longer visible.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100886\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete.png\" alt=\"nodejs fs delete file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-fs-delete-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>Rename File:<\/h4>\n<p>If we want to rename a file we will use the fs.rename() method of the file system module.<\/p>\n<p><strong>Code for renaming a file:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require('fs');\r\nfs.rename('DataFlair1.txt', 'DataFlair2.txt', function (err, file) {\r\n    if (err) throw err;\r\n    console.log('Saved!');\r\n});\r\n<\/pre>\n<h3>Important nodejs modules and their uses:<\/h3>\n<p>1. <strong>Http<\/strong>: Used for creating an HTTP server in nodejs.<\/p>\n<p>2. <strong>Assert<\/strong>: Used for testing node js application using a set of assertion functions.<\/p>\n<p>3. <strong>Fs<\/strong>: Used in modification of files.<\/p>\n<p>4. <strong>Path<\/strong>: It helps to find the file paths.<\/p>\n<p>5. <strong>Process<\/strong>: Gives information regarding the current process.<\/p>\n<p>6. <strong>Os<\/strong>: It contains the details of the operating system in which the node js application is currently running.<\/p>\n<p>7. <strong>Querystring<\/strong>: It helps in parsing and proper formatting of the url.<\/p>\n<p>8. <strong>Url<\/strong>: This module also helps in parsing the urls.<\/p>\n<h3>Conclusion:<\/h3>\n<p>In this blog we discussed what are modules, its types, uses, and also seen the Http, url and fs modules. Hope you loved reading it and don&#8217;t forget to check DataFlair\u2019s other blogs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modules are encapsulated code blocks that communicate with an external application. These can be a single file or a collection of multiple files\/folders. These are reusable, hence they are widely used. Let us learn&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":100875,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25265,25270,25268,25266,25264,25267,25269],"class_list":["post-100676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-nodejs-core-modules","tag-nodejs-file-system-module","tag-nodejs-http-module","tag-nodejs-local-modules","tag-nodejs-modules","tag-nodejs-third-party-modules","tag-nodejs-url-module"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nodejs Modules - Types and Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"See various modules of nodejs like core modules, local modules and third party modules. Also learn about seen the Http, url and fs modules.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/nodejs-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nodejs Modules - Types and Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"See various modules of nodejs like core modules, local modules and third party modules. Also learn about seen the Http, url and fs modules.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-04T03:30:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-04T07:09:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-module.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nodejs Modules - Types and Examples - DataFlair","description":"See various modules of nodejs like core modules, local modules and third party modules. Also learn about seen the Http, url and fs modules.","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:\/\/data-flair.training\/blogs\/nodejs-modules\/","og_locale":"en_US","og_type":"article","og_title":"Nodejs Modules - Types and Examples - DataFlair","og_description":"See various modules of nodejs like core modules, local modules and third party modules. Also learn about seen the Http, url and fs modules.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-10-04T03:30:06+00:00","article_modified_time":"2021-10-04T07:09:21+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-module.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Nodejs Modules &#8211; Types and Examples","datePublished":"2021-10-04T03:30:06+00:00","dateModified":"2021-10-04T07:09:21+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/"},"wordCount":1213,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-module.jpg","keywords":["Nodejs Core modules","Nodejs File System Module","Nodejs HTTP Module","Nodejs Local modules","Nodejs Modules","Nodejs Third party modules","Nodejs URL Module"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-modules\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/","name":"Nodejs Modules - Types and Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-module.jpg","datePublished":"2021-10-04T03:30:06+00:00","dateModified":"2021-10-04T07:09:21+00:00","description":"See various modules of nodejs like core modules, local modules and third party modules. Also learn about seen the Http, url and fs modules.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-modules\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-module.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-module.jpg","width":1200,"height":628,"caption":"nodejs modules"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Nodejs Modules &#8211; Types and Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=100676"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100676\/revisions"}],"predecessor-version":[{"id":100888,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100676\/revisions\/100888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/100875"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=100676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=100676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=100676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}