

{"id":105008,"date":"2021-12-20T09:00:42","date_gmt":"2021-12-20T03:30:42","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=105008"},"modified":"2021-12-20T12:12:28","modified_gmt":"2021-12-20T06:42:28","slug":"create-first-nodejs-application","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/","title":{"rendered":"Create Your First Node.js Application"},"content":{"rendered":"<p>Today we will be discussing how we can create a nodejs application. We will first create a nodejs server then we will use a web browser to access the server.<\/p>\n<h3>Components of nodejs application:<\/h3>\n<p><strong>1. Importing modules<\/strong>: To include anything in nodejs we use the require() function.<\/p>\n<p><strong>2. Creating server:<\/strong> This will listen to the client requests. Mostly http methods to create the server.<\/p>\n<p><strong>3. Request and response:<\/strong> The server will listen to the http requests made by the client and in turn will send a response for that request.<\/p>\n<h3>Importing Module:<\/h3>\n<p>At first we will be importing all the modules that we will be using. For this application we are using the http module so we will include it in our project file using require function()<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let http=require(\u2018http\u2019)<\/pre>\n<h3>Creating the server:<\/h3>\n<p>We will be using the create http server method to create our server, next we will be using the listen method to set our port to 3000. The server will return a \u201cwelcome to DataFlair\u201d string to the user.<\/p>\n<h4>Code for creating a server:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let http = require(\"http\");\r\n \r\nhttp.createServer(function (req, res) {\r\n    res.writeHead(200, { 'Content-Type': 'text\/plain' });\r\n    res.end('welcome to DataFlair\\n');\r\n}).listen(3000);\r\nconsole.log('Server is running on port 3000');\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-create-server-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105030\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-create-server-output.webp\" alt=\"nodejs create server\" width=\"1366\" height=\"690\" \/><\/a><\/p>\n<h3>Testing request and response:<\/h3>\n<p>In order to test your code first you run the server using the below command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">node index.js<\/pre>\n<p>Now if you see the below output then you have successfully set up your server.<\/p>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-testing-request-response.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105031\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-testing-request-response.webp\" alt=\"nodejs testing request response\" width=\"1366\" height=\"690\" \/><\/a><\/p>\n<h3>Make a request to the server:<\/h3>\n<p>Now go to any web browser on your system and go to localhost:3000 url. You will see the welcome to DataFlair text.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-request-server-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105032\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-request-server-1.webp\" alt=\"nodejs request server\" width=\"1366\" height=\"690\" \/><\/a><\/p>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have discussed how to create a nodejs application. I hope you were able to properly setup and run your server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we will be discussing how we can create a nodejs application. We will first create a nodejs server then we will use a web browser to access the server. Components of nodejs application:&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":105029,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25991,25990],"class_list":["post-105008","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-first-node-js-application","tag-node-js-application"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create Your First Node.js Application - DataFlair<\/title>\n<meta name=\"description\" content=\"Create your first Node.js Application. Learn how to import necessary modules, create server, test request &amp; response, Make request to server.\" \/>\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\/create-first-nodejs-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Your First Node.js Application - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Create your first Node.js Application. Learn how to import necessary modules, create server, test request &amp; response, Make request to server.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/\" \/>\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-20T03:30:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-20T06:42:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-first-application.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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Your First Node.js Application - DataFlair","description":"Create your first Node.js Application. Learn how to import necessary modules, create server, test request & response, Make request to server.","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\/create-first-nodejs-application\/","og_locale":"en_US","og_type":"article","og_title":"Create Your First Node.js Application - DataFlair","og_description":"Create your first Node.js Application. Learn how to import necessary modules, create server, test request & response, Make request to server.","og_url":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-12-20T03:30:42+00:00","article_modified_time":"2021-12-20T06:42:28+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-first-application.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Create Your First Node.js Application","datePublished":"2021-12-20T03:30:42+00:00","dateModified":"2021-12-20T06:42:28+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/"},"wordCount":266,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-first-application.webp","keywords":["First Node.js Application","Node.js Application"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/","url":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/","name":"Create Your First Node.js Application - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-first-application.webp","datePublished":"2021-12-20T03:30:42+00:00","dateModified":"2021-12-20T06:42:28+00:00","description":"Create your first Node.js Application. Learn how to import necessary modules, create server, test request & response, Make request to server.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-first-application.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-first-application.webp","width":1200,"height":628,"caption":"nodejs first application"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/create-first-nodejs-application\/#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":"Create Your First Node.js Application"}]},{"@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\/105008","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=105008"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/105008\/revisions"}],"predecessor-version":[{"id":105033,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/105008\/revisions\/105033"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/105029"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=105008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=105008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=105008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}