{"id":87800,"date":"2023-06-14T10:05:06","date_gmt":"2023-06-14T04:35:06","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=87800"},"modified":"2023-06-14T10:05:06","modified_gmt":"2023-06-14T04:35:06","slug":"web-module-nodejs","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/","title":{"rendered":"Node.js Web Module"},"content":{"rendered":"<p>One of the key features of Node.js is its extensive module system, which provides various modules to enhance and simplify web development. In this article, we will delve into the Web module in Node.js and explore its capabilities.<\/p>\n<h3>What is Web Server?<\/h3>\n<p>A web server is a software application that serves as the foundation for hosting and delivering websites, web applications, and other online content over the internet. It acts as a mediator between a client (such as a web browser) and the requested web resources, facilitating the exchange of information between them.<\/p>\n<p>The primary function of a web server is to handle and respond to incoming HTTP (Hypertext Transfer Protocol) requests from clients. When a user enters a URL in their browser or clicks on a hyperlink, the browser sends an HTTP request to the appropriate web server. The web server processes the request, retrieves the requested resources (such as HTML files, images, or data), and sends a response back to the client.<\/p>\n<p>Here are some key aspects and functionalities of a web server:<\/p>\n<ul>\n<li>Routing and Resource Handling<\/li>\n<li>HTTP Request Processing<\/li>\n<li>Response Generation<\/li>\n<li>Content Delivery<\/li>\n<li>Security and Authentication<\/li>\n<li>Performance and Scalability<\/li>\n<\/ul>\n<p>Examples of popular web servers include Apache HTTP Server, Nginx, Microsoft IIS (Internet Information Services), and Node.js (when used as a web server). These servers often come with additional features and modules that extend their functionality, such as URL rewriting, proxying, compression, and more.<\/p>\n<h3>Nodejs Web Module<\/h3>\n<p>The Web module, also known as the HTTP module, is a core module in Node.js that enables developers to create web servers and handle HTTP requests and responses. It provides a straightforward API for building robust web applications. Let&#8217;s dive into the key components and functionalities of the Web module.<\/p>\n<h3>Setting Up a Basic Web Server:<\/h3>\n<p>To get started, we need to import the Web module using the \u2018require\u2019 statement:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const http = require('http');<\/pre>\n<p>Next, we can create a basic web server using the \u2018http.createServer()\u2019 method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const server = http.createServer((req, res) =&gt; {\n  \/\/ handle requests here\n});\n<\/pre>\n<p>The \u2018createServer()\u2019 method takes a callback function that will be executed whenever a request is made to the server. Inside the callback, we can define how the server should respond to different types of requests.<\/p>\n<h3>Handling Requests and Sending Responses in nodejs:<\/h3>\n<p>The callback function in the \u2018createServer()\u2019 method receives two parameters: \u2018req\u2019 (the incoming request) and \u2018res\u2019 (the outgoing response). We can use these objects to handle requests and send appropriate responses.<\/p>\n<p>Let&#8217;s look at an example that sends a simple &#8220;Hello, World!&#8221; response for all requests:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const server = http.createServer((req, res) =&gt; {\n  res.statusCode = 200;\n  res.setHeader('Content-Type', 'text\/plain');\n  res.end('Hello, World!');\n});\n<\/pre>\n<p>In this example, we set the status code to 200 (indicating a successful response), set the \u2018Content-Type\u2019 header to text\/plain, and send the response body using the res.end() method.<\/p>\n<h3>Starting the Server:<\/h3>\n<p>Once we have created the server, we need to start it to listen for incoming requests. We can specify the port number on which the server should listen:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const port = 3000;\nserver.listen(port, () =&gt; {\n  console.log(`Server running on port ${port}`);\n});\n<\/pre>\n<p>In this case, the server will listen on port 3000. You can change it to any available port of your choice.<\/p>\n<h3>Handling Different Routes:<\/h3>\n<p>To handle different routes, we can examine the \u2018req.url\u2019 property and define separate logic for each route. Let&#8217;s consider an example where we have two routes: \u2018\/home\u2019 and \u2018\/abou\u2019t:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const server = http.createServer((req, res) =&gt; {\n  if (req.url === '\/home') {\n    res.statusCode = 200;\n    res.setHeader('Content-Type', 'text\/plain');\n    res.end('Welcome to the home page!');\n  } else if (req.url === '\/about') {\n    res.statusCode = 200;\n    res.setHeader('Content-Type', 'text\/plain');\n    res.end('This is the about page.');\n  } else {\n    res.statusCode = 404;\n    res.end('Page not found.');\n  }\n});\n<\/pre>\n<p>In this example, we check the value of \u2018req.url\u2019 and send different responses accordingly. If the route is not found, we set the status code to 404 and send an appropriate message.<\/p>\n<h3>Conclusion:<\/h3>\n<p>The Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. In this article, we explored the basics of creating a web server, handling requests, and sending responses.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the key features of Node.js is its extensive module system, which provides various modules to enhance and simplify web development. In this article, we will delve into the Web module in Node.js&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":87802,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4984],"tags":[5054],"class_list":["post-87800","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.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.js Web Module - TechVidvan<\/title>\n<meta name=\"description\" content=\"Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. Learn more about it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/\" \/>\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 - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. Learn more about it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-14T04:35:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/05\/node-js-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=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js Web Module - TechVidvan","description":"Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. Learn more about it.","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:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/","og_locale":"en_US","og_type":"article","og_title":"Node.js Web Module - TechVidvan","og_description":"Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. Learn more about it.","og_url":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-06-14T04:35:06+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/05\/node-js-web-module.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Node.js Web Module","datePublished":"2023-06-14T04:35:06+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/"},"wordCount":606,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/05\/node-js-web-module.webp","keywords":["Node.js Web Module"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/","url":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/","name":"Node.js Web Module - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/05\/node-js-web-module.webp","datePublished":"2023-06-14T04:35:06+00:00","description":"Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. Learn more about it.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/05\/node-js-web-module.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/05\/node-js-web-module.webp","width":1200,"height":628,"caption":"node js web module"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/web-module-nodejs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Node.js Web Module"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=87800"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87800\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/87802"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=87800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=87800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=87800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}