{"id":924,"date":"2023-01-08T23:01:08","date_gmt":"2023-01-08T22:01:08","guid":{"rendered":"https:\/\/gregarednak.si\/?p=924"},"modified":"2023-07-13T14:37:07","modified_gmt":"2023-07-13T12:37:07","slug":"node-js-express-hello-world-aplikacija","status":"publish","type":"post","link":"https:\/\/gregarednak.si\/en\/node-js-express-hello-world-aplikacija\/","title":{"rendered":"Node.js-Express \u201chello world\u201d application"},"content":{"rendered":"<p>An example of a simple application built using the Node.js runtime and the Express framework.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Node.js?<\/h2>\n\n\n\n<p><strong>Node.js <\/strong>is <strong>open source runtime environment<\/strong> for application development on <strong>server side<\/strong> in <strong>JavaScript programming language<\/strong>It runs on various platforms and allows JavaScript code to be executed on a server outside of a web browser. It is commonly used to build APIs, real-time applications, and microservices.<\/p>\n\n\n\n<p>Node.js also includes a package manager <strong>NPM <\/strong>(Node Package Manager), which you can use to install and manage third-party packages (libraries and frameworks) for your Node.js projects. To install a package using the npm package manager, you can use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install packageName<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Node.js and Express for a \u201chello world\u201d application <\/h2>\n\n\n\n<p>To start using Node.js, you will need to install it on your computer. You can download Node.js from the official website <a href=\"https:\/\/nodejs.org\/\">https:\/\/nodejs.org\/<\/a> and install it according to the instructions provided. Once you have Node.js installed, you can use it to run JavaScript files from the command line (CMD, terminal, powershell, etc.).<\/p>\n\n\n\n<p>To develop a simple Node.js application, you will need to have Node.js installed and create a new project. You can do this with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm init npm install express<\/code><\/pre>\n\n\n\n<pre id=\"tw-target-text\" class=\"wp-block-preformatted\">Then create a file, usually named server.js, and add the code:<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>const express = require(&#039;express&#039;); const app = express(); app.get(&#039;\/&#039;, (req, res) =&gt; { res.send(&#039;Hello World!&#039;); }); app.listen(3000, () =&gt; { console.log(&#039;Server listening on port 3000&#039;); });<\/code><\/pre>\n\n\n\n<p>This code creates an Express server that listens for HTTP requests on port 3000. When it receives a request, it sends a response with the text \u201cHello World!\u201d. To start the server, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node server.js<\/code><\/pre>\n\n\n\n<p>You should see a message in the console that the server is listening on port 3000 (e.g. \u201cServer listening on port 3000\u201d).<\/p>\n\n\n\n<p>To test how the running server works, open a web browser and go to http:\/\/localhost:3000. You should see the message \u201cHello World!\u201d displayed in the browser.<\/p>\n\n\n\n<p>This is a very basic example of an application built with Node.js and Express. You can add multiple routes to the application with different functionality and have it respond differently by defining additional endpoints and handling different HTTP methods (e.g. POST, PUT, DELETE).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"502\" height=\"203\" src=\"https:\/\/gregarednak.si\/wp-content\/uploads\/2023\/07\/Hello-world.gif\" alt=\"\" class=\"wp-image-2059\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p><strong>Node.js <\/strong>is <strong>open source runtime environment<\/strong>, which runs on the server side, meaning it does not require a web browser. It is often used to build real-time applications or applications that work through APIs. <a href=\"https:\/\/gregarednak.si\/en\/\" title=\"Website development and IT services \u2013 Grega Rednak\">Website development<\/a> is suitable with this tool because it is fast and efficient. It is also often used in microservices architecture.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Sources:<\/h4>\n\n\n\n<p> <a href=\"https:\/\/nodejs.org\/\">https:\/\/nodejs.org\/<\/a> <\/p>","protected":false},"excerpt":{"rendered":"<p>An example of how to create a simple \u201chello world\u201d application built using the Node.js runtime and the Express framework.<\/p>","protected":false},"author":1,"featured_media":1118,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programiranje"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/posts\/924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":0,"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/media\/1118"}],"wp:attachment":[{"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregarednak.si\/en\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}