{"id":4206,"date":"2023-11-04T23:14:08","date_gmt":"2023-11-04T23:14:08","guid":{"rendered":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/"},"modified":"2023-11-05T05:47:57","modified_gmt":"2023-11-05T05:47:57","slug":"building-web-applications-with-node-js","status":"publish","type":"post","link":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/","title":{"rendered":"Building Web Applications with Node.js"},"content":{"rendered":"

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to create server-side applications with ease. It is designed to facilitate scalable and efficient networking and give them a high-performance platform to work with.<\/p>\n

In this tutorial, we will look at building a web application with Node.js \u2013 covering from setting up your development environment to building a feature-rich web application.<\/p>\n

Setting up the Development Environment<\/h2>\n

Before we start building our web application, we must ensure that our development environment is set up correctly. Here\u2019s how you can set up your development environment:<\/p>\n

    \n
  1. Install Node.js<\/li>\n<\/ol>\n

    First, you need to install Node.js \u2013 you can visit the official website(https:\/\/nodejs.org\/en\/) and download the Node.js installer for your operating system.<\/p>\n

      \n
    1. Install a Text Editor or Integrated Development Environment (IDE)<\/li>\n<\/ol>\n

      Next, you need a text editor or integrated development environment (IDE) to write your code. There are many options available, including Visual Studio Code(https:\/\/code.visualstudio.com\/), Sublime Text(https:\/\/www.sublimetext.com\/), and Atom(https:\/\/atom.io\/).<\/p>\n

        \n
      1. Create a New Directory for your Web Application<\/li>\n<\/ol>\n

        Now that you have your development environment set up, create a new directory for your web application and navigate to it in your terminal or command prompt.<\/p>\n

          \n
        1. Initialize the Project with NPM<\/li>\n<\/ol>\n

          With your directory in place, you now need to initialize a new project with NPM (Node Package Manager). To do this, you need to input the following command in your terminal or command prompt:<\/p>\n

          npm init\n<\/code><\/pre>\n

          This command will prompt you to provide details about your project, such as the project\u2019s name, version, and description.<\/p>\n

          Once you\u2019ve provided these details, npm will create your package.json file \u2013 this file contains information about your project and the dependencies it requires.<\/p>\n

          Setting up Basic Server with Node.js<\/h2>\n

          Now that you\u2019ve initialized your project, you can begin building your web application. The first step is to set up a basic server. Here\u2019s how you can set up your server:<\/p>\n

            \n
          1. Install the Express Framework<\/li>\n<\/ol>\n

            The Express framework is a popular choice for building web applications with Node.js. You can install the Express framework by typing the following command in your terminal or command prompt:<\/p>\n

            npm install express --save\n<\/code><\/pre>\n
              \n
            1. Create your server.js file<\/li>\n<\/ol>\n

              Now you need to create a new file in your project directory and name it server.js.<\/p>\n

                \n
              1. Write your Code<\/li>\n<\/ol>\n

                With your server.js file created, it\u2019s time to write code to set up your server. Let\u2019s start with your code by requiring the Express framework and setting up your server to listen to incoming requests:<\/p>\n

                const express = require('express');\n\nconst app = express();\n\napp.listen(3000, () => console.log('Server started at http:\/\/localhost:3000'));\n<\/code><\/pre>\n

                In this code block, you require the express module and set the app variable to an instance of Express. Then, you set up your server to listen for incoming requests on port 3000 and log a message to the console to let you know that the server has started.<\/p>\n

                  \n
                1. Start your server<\/li>\n<\/ol>\n

                  Now navigate to your project directory in your terminal or command prompt and type the following command to start your server:<\/p>\n

                  node server.js\n<\/code><\/pre>\n

                  You should now be able to view your web application by navigating to http:\/\/localhost:3000 in your web browser.<\/p>\n

                  Creating Routes with Express<\/h2>\n

                  Now that you\u2019ve set up a basic server, you need to create routes for your web application. Here\u2019s how you can set up routes:<\/p>\n

                    \n
                  1. Create a new file in your project directory and name it routes.js.<\/p>\n<\/li>\n
                  2. \n

                    Write your Code<\/p>\n<\/li>\n<\/ol>\n

                    With your routes.js file created, it\u2019s time to write code to set up your routes. Let\u2019s start with your code by requiring the Express framework and setting up your routes:<\/p>\n

                    const express = require('express');\n\nconst router = express.Router();\n\nrouter.get('\/', (req, res) => {\n  res.send('Hello World!');\n});\n\nmodule.exports = router;\n<\/code><\/pre>\n

                    In this code block, you have required the express module and set up your routes using the router object. Next, you have set up a GET request to handle the root path of your web application \u2013 when a user navigates to the root path, the callback function in this code block will execute, and the response that is sent to the client will be \u2018Hello World!\u2019.<\/p>\n

                      \n
                    1. Integrate Your Routes with Server<\/li>\n<\/ol>\n

                      Now that you\u2019ve created your routes, you need to integrate them with your server. You can do this by requiring your routes.js file in your server.js file and using the app.use()<\/code> method to apply your routes:<\/p>\n

                      const express = require('express');\nconst routes = require('.\/routes');\n\nconst app = express();\n\napp.use('\/', routes);\n\napp.listen(3000, () => console.log('Server started at http:\/\/localhost:3000'));\n<\/code><\/pre>\n

                      In this code block, you have required your routes.js file and used the app.use()<\/code> method to apply your routes to the root path of your web application.<\/p>\n

                        \n
                      1. Test Your Routes<\/li>\n<\/ol>\n

                        With your routes set up, it\u2019s time to test them. Start your server with the node server.js<\/code> command, and then navigate to http:\/\/localhost:3000 in your web browser \u2013 if everything is set up correctly, you should see the \u2018Hello World!\u2019 message display on your webpage.<\/p>\n

                        Building a Web Application with Node.js<\/h2>\n

                        Now that you\u2019ve created routes and set up your server, it\u2019s time to build a feature-rich web application. Here\u2019s how you can build your web application:<\/p>\n

                          \n
                        1. Install EJS<\/li>\n<\/ol>\n

                          EJS is a popular templating engine that lets you generate HTML markup with plain JavaScript. You can install EJS by typing the following command in your terminal or command prompt:<\/p>\n

                          npm install ejs --save\n<\/code><\/pre>\n
                            \n
                          1. Create a Views Directory<\/li>\n<\/ol>\n

                            With EJS installed, you need to create a new directory in your project directory and name it views \u2013 this directory will store your EJS templates.<\/p>\n

                              \n
                            1. Create an EJS Template<\/li>\n<\/ol>\n

                              Create a new file in your views directory and name it index.ejs. In this file, you need to write code that generates your HTML markup. Here\u2019s an example:<\/p>\n

                              <!DOCTYPE html>\n<html>\n\n<head>\n  <title><%= title %><\/title>\n<\/head>\n\n<body>\n  <h1>Welcome to <%= title %><\/h1>\n\n  <ul>\n    <% for(let i = 0; i < names.length; i++) { %>\n      <li><%= names[i] %><\/li>\n    <% } %>\n  <\/ul>\n<\/body>\n\n<\/html>\n<\/code><\/pre>\n

                              In this code block, you have set up an HTML page with a dynamic title and a list of names. The title and names are dynamically generated with the EJS templating engine.<\/p>\n

                                \n
                              1. Update Your Routes<\/li>\n<\/ol>\n

                                With your EJS template created, you need to update your routes to render the template. Here\u2019s how you can do it:<\/p>\n

                                const express = require('express');\n\nconst router = express.Router();\n\nrouter.get('\/', (req, res) => {\n  const data = {\n    title: 'My Website',\n    names: ['John', 'Jane', 'Doe'],\n  };\n\n  res.render('index', data);\n});\n\nmodule.exports = router;\n<\/code><\/pre>\n

                                In this code block, you have created a data object with a title and an array of names. Next, you have passed the index.ejs template and the data object to the res.render()<\/code> method \u2013 this method renders your EJS template with the data passed in and generates the HTML markup.<\/p>\n

                                  \n
                                1. Test Your Web Application<\/li>\n<\/ol>\n

                                  With your web application built, it\u2019s time to test it. Start your server with the node server.js<\/code> command, and then navigate to http:\/\/localhost:3000 in your web browser \u2013 if everything is set up correctly, you should see your web application display on your webpage.<\/p>\n

                                  Conclusion<\/h2>\n

                                  In this tutorial, we have looked at building a web application with Node.js \u2013 from setting up your development environment to building a feature-rich web application. We covered how to set up a server with Express, create routes, and generate HTML markup with the EJS templating engine \u2013 giving you a solid foundation to build on.<\/p>\n

                                  With Node.js and its many libraries and frameworks, you can build highly performant and scalable web applications in no time. So go ahead and give it a try \u2013 and feel free to reach out to the community if you need any help. Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"

                                  Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to create server-side applications with ease. It is designed to facilitate scalable and efficient networking and give them a high-performance platform to work with. In this tutorial, we will look at building a web application with Node.js \u2013 covering Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[457,156,19,290,1679,1577,49,698],"yoast_head":"\nBuilding Web Applications with Node.js - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Building Web Applications with Node.js\" \/>\n<meta property=\"og:description\" content=\"Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to create server-side applications with ease. It is designed to facilitate scalable and efficient networking and give them a high-performance platform to work with. In this tutorial, we will look at building a web application with Node.js \u2013 covering Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:14:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:47:57+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"Building Web Applications with Node.js\",\n\t \"datePublished\": \"2023-11-04T23:14:08+00:00\",\n\t \"dateModified\": \"2023-11-05T05:47:57+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\"\n\t },\n\t \"wordCount\": 1175,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"backend development\\\"]\",\n\t \"\\\"JavaScript\\\"\",\n\t \"\\\"Node.js\\\"\",\n\t \"\\\"server-side programming\\\"\",\n\t \"\\\"Web Application Frameworks\\\"]\",\n\t \"\\\"web development tools\\\"]\",\n\t \"\\\"Web development\\\"\",\n\t \"[\\\"Building web applications\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\",\n\t \"url\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\",\n\t \"name\": \"Building Web Applications with Node.js - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:14:08+00:00\",\n\t \"dateModified\": \"2023-11-05T05:47:57+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/building-web-applications-with-node-js\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"Building Web Applications with Node.js\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Building Web Applications with Node.js - Pantherax Blogs","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":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/","og_locale":"en_US","og_type":"article","og_title":"Building Web Applications with Node.js","og_description":"Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to create server-side applications with ease. It is designed to facilitate scalable and efficient networking and give them a high-performance platform to work with. In this tutorial, we will look at building a web application with Node.js \u2013 covering Continue Reading","og_url":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:14:08+00:00","article_modified_time":"2023-11-05T05:47:57+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"Building Web Applications with Node.js","datePublished":"2023-11-04T23:14:08+00:00","dateModified":"2023-11-05T05:47:57+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/"},"wordCount":1175,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"backend development\"]","\"JavaScript\"","\"Node.js\"","\"server-side programming\"","\"Web Application Frameworks\"]","\"web development tools\"]","\"Web development\"","[\"Building web applications\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/","url":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/","name":"Building Web Applications with Node.js - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:14:08+00:00","dateModified":"2023-11-05T05:47:57+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/building-web-applications-with-node-js\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/building-web-applications-with-node-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"Building Web Applications with Node.js"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4206"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=4206"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4206\/revisions"}],"predecessor-version":[{"id":4342,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4206\/revisions\/4342"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=4206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=4206"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=4206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}