

{"id":104981,"date":"2021-12-17T09:00:44","date_gmt":"2021-12-17T03:30:44","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=104981"},"modified":"2021-12-17T12:16:41","modified_gmt":"2021-12-17T06:46:41","slug":"nodejs-web-module","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/","title":{"rendered":"Node.js Web Module"},"content":{"rendered":"<p>In this article, we will be discussing about Node.js web Module, web server, how to create a web server, how to create a web client in detail with code and examples.<\/p>\n<h3>Web server and its working:<\/h3>\n<p>Web servers are programs that deal with http requests. The http requests sent by the client are handled by the web server and it then gives a response to the clients. A web server generally provides the html, stylesheets, and images.<\/p>\n<h3>Web Server Architecture:<\/h3>\n<h4>1. Client:<\/h4>\n<p>Browsers and other applications that make http requests to the server are known as clients.<\/p>\n<h4>2. Server:<\/h4>\n<p>This layer takes the requests made by the client and sends them a response.<\/p>\n<h4>3. Business:<\/h4>\n<p>This layer contains all the logics and functions required in the application, it also interacts with the data layer using databases.<\/p>\n<h4>4. Data:<\/h4>\n<p>This layer contains the data required by the application. The data is generally stored in the databases.<\/p>\n<h3>Create a Web server:<\/h3>\n<p>We will be creating a nodejs server using http which is a builtin module of node js.<\/p>\n<p><strong>Code for creating a web server:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const http = require('http');\r\nhttp.createServer(function (request, response) {\r\n    response.write(\"Welcome to DataFlair\");\r\n    response.end();\r\n}).listen(3000);\r\nconsole.log('Server is running on port 3000');<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-server.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105019\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-server.webp\" alt=\"nodejs web server\" width=\"1366\" height=\"705\" \/><\/a><\/p>\n<h3>Making a request to node server:<\/h3>\n<p>Open your browser and go to localhost:3000 you will see like the below image:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-request-server.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105020\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-request-server.webp\" alt=\"nodejs request server\" width=\"1366\" height=\"705\" \/><\/a><\/p>\n<p><strong>Output at terminal:<\/strong><\/p>\n<div class=\"code-output\">Server is running on port 3000<\/div>\n<h3>Creating web client:<\/h3>\n<p>Till now, we have seen creating a web server. Now we will see how to create a web client, here we will be creating the web client using the built in http module. In order to make a request from the web client to our web server we will be running the below code from a different terminal and make sure that your web server is running before making a request from your web client.<\/p>\n<h3>Code for creating a web Client:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const http = require('http');\r\nconst options = {\r\n    host: 'localhost',\r\n    port: '3000',\r\n    path: '\/'\r\n};\r\nconst callback = function (response) {\r\n    var body = '';\r\n    response.on('data', function (data) {\r\n        body += data;\r\n    });\r\n    response.on('end', function () {\r\n        console.log(body);\r\n    });\r\n}\r\nvar req = http.request(options, callback);\r\nreq.end();\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-client.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105021\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-client.webp\" alt=\"nodejs web client\" width=\"1366\" height=\"705\" \/><\/a><\/p>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have seen Nodejs web modules in detail. Hope you liked it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be discussing about Node.js web Module, web server, how to create a web server, how to create a web client in detail with code and examples. Web server and&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":105018,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25985],"class_list":["post-104981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-node-js-web-module"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.js Web Module - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about Node.js web module. See Nodejs web server, its architecture &amp; working. Learn to create web server and web client etc.\" \/>\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-web-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js Web Module - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about Node.js web module. See Nodejs web server, its architecture &amp; working. Learn to create web server and web client etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/\" \/>\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-12-17T03:30:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-17T06:46:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-module.webp\" \/>\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\/webp\" \/>\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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js Web Module - DataFlair","description":"Learn about Node.js web module. See Nodejs web server, its architecture & working. Learn to create web server and web client etc.","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-web-module\/","og_locale":"en_US","og_type":"article","og_title":"Node.js Web Module - DataFlair","og_description":"Learn about Node.js web module. See Nodejs web server, its architecture & working. Learn to create web server and web client etc.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-12-17T03:30:44+00:00","article_modified_time":"2021-12-17T06:46:41+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-module.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Node.js Web Module","datePublished":"2021-12-17T03:30:44+00:00","dateModified":"2021-12-17T06:46:41+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/"},"wordCount":313,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-module.webp","keywords":["Node.js Web Module"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/","name":"Node.js Web Module - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-module.webp","datePublished":"2021-12-17T03:30:44+00:00","dateModified":"2021-12-17T06:46:41+00:00","description":"Learn about Node.js web module. See Nodejs web server, its architecture & working. Learn to create web server and web client etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-web-module\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-module.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-web-module.webp","width":1200,"height":628,"caption":"nodejs web module"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-web-module\/#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":"Node.js Web Module"}]},{"@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\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104981","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=104981"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104981\/revisions"}],"predecessor-version":[{"id":106792,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104981\/revisions\/106792"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/105018"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=104981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=104981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=104981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}