{"id":88123,"date":"2023-08-18T19:07:35","date_gmt":"2023-08-18T13:37:35","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=88123"},"modified":"2023-08-18T19:07:35","modified_gmt":"2023-08-18T13:37:35","slug":"nodejs-first-application","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/","title":{"rendered":"Node.JS First Application"},"content":{"rendered":"<p>Node.js provides a built-in HTTP module that allows developers to create web servers and handle HTTP requests and responses. In this article, we will walk through the process of creating a simple Node.js application using the HTTP module. By the end, you&#8217;ll have a basic understanding of how to set up a server, handle requests, and send responses.<\/p>\n<h3>Prerequisites for First Application in NodeJS:<\/h3>\n<p>Before we begin, make sure you have Node.js installed on your machine. You can download the latest version from the official Node.js website (https:\/\/nodejs.org) and follow the installation instructions for your operating system.<\/p>\n<h4>Step 1 Set Up Your Project:<\/h4>\n<p>1. Create a new folder for your project and navigate to it using a command prompt or terminal.<\/p>\n<p>2. Initialize a new Node.js project by running the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">npm init -y\n<\/pre>\n<p>This command creates a package.json file that tracks your project&#8217;s dependencies and configuration.<\/p>\n<h4>Step 2 Write Your Application Code:<\/h4>\n<p>Create a new file, \u2018app.js\u2019, in your project folder.<\/p>\n<p>Open \u2018app.js\u2019 in your preferred text editor or integrated development environment (IDE) and add the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const http = require('http');\n \nconst server = http.createServer((req, res) =&gt; {\n  \/\/ Set the response headers\n  res.writeHead(200, { 'Content-Type': 'text\/plain' });\n \n  \/\/ Send a response to the client\n  res.end('Hello, World!');\n});\n \nconst port = 3000;\nserver.listen(port, () =&gt; {\n  console.log(`Server is running on http:\/\/localhost:${port}`);\n});\n<\/pre>\n<p>In this code, we import the \u2018http\u2019 module and use its \u2018createServer\u2019 method to create an HTTP server. The server object listens for incoming requests and responds with the message &#8220;Hello, World!&#8221;.<\/p>\n<p>We set the response headers using \u2018res.writeHead\u2019 with a status code of \u2018200\u2019 and the content type as \u2018text\/plain\u2019. Finally, we send the response back to the client using \u2018res.end\u2019.<\/p>\n<p>The server listens on port 3000 using the \u2018listen\u2019 method. The callback function is executed once the server starts, and it logs a message to the console indicating that the server is running.<\/p>\n<h4>Step 3 Run Your Application:<\/h4>\n<p>1. Open a command prompt or terminal and navigate to your project folder.<\/p>\n<p>2. Start your application by running the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node app.js\n<\/pre>\n<p>3. Open your web browser and visit http:\/\/localhost:3000. You should see the message &#8220;Hello, World!&#8221; displayed on the page.<\/p>\n<p><strong>OUTPUT<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/08\/NodeJS-First-Application-output-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88309 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/08\/NodeJS-First-Application-output-1.webp\" alt=\"NodeJS First Application output\" width=\"1022\" height=\"299\" \/><\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>In this article, we learned how to create a basic Node.js application using the HTTP module. We set up a server, handled incoming requests, and sent responses back to the client. The HTTP module provides a foundation for building more complex web applications in Node.js. Feel free to experiment and extend this application to explore more features and functionalities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node.js provides a built-in HTTP module that allows developers to create web servers and handle HTTP requests and responses. In this article, we will walk through the process of creating a simple Node.js application&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":88125,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4984],"tags":[5140,5141,5110,5142],"class_list":["post-88123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-first-application","tag-first-application-in-nodejs","tag-nodejs","tag-nodejs-in-first-application"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.JS First Application - TechVidvan<\/title>\n<meta name=\"description\" content=\"In this article, we will walk through the process of creating a simple Node.js application using the HTTP module.\" \/>\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\/nodejs-first-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.JS First Application - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In this article, we will walk through the process of creating a simple Node.js application using the HTTP module.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/\" \/>\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-08-18T13:37:35+00:00\" \/>\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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.JS First Application - TechVidvan","description":"In this article, we will walk through the process of creating a simple Node.js application using the HTTP module.","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\/nodejs-first-application\/","og_locale":"en_US","og_type":"article","og_title":"Node.JS First Application - TechVidvan","og_description":"In this article, we will walk through the process of creating a simple Node.js application using the HTTP module.","og_url":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-08-18T13:37:35+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Node.JS First Application","datePublished":"2023-08-18T13:37:35+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/"},"wordCount":391,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#primaryimage"},"thumbnailUrl":"","keywords":["First Application","First Application in NodeJS","Nodejs","NodeJS in First Application"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/","url":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/","name":"Node.JS First Application - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-08-18T13:37:35+00:00","description":"In this article, we will walk through the process of creating a simple Node.js application using the HTTP module.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/nodejs-first-application\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Node.JS First Application"}]},{"@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\/88123","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=88123"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}