{"id":4204,"date":"2023-05-14T09:56:11","date_gmt":"2023-05-14T09:56:11","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4204"},"modified":"2023-06-10T11:29:30","modified_gmt":"2023-06-10T11:29:30","slug":"node-js-getting-started","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/node-js-getting-started\/","title":{"rendered":"Node.js &#8211; Getting Started"},"content":{"rendered":"<h2>Download Node.js<\/h2>\n<p>The official Node.js website has installation instructions for Node.js:\u00a0<a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/nodejs.org<\/a><\/p>\n<hr \/>\n<h2>Getting Started<\/h2>\n<p>Once you have downloaded and installed Node.js on your computer, let&#8217;s try to display &#8220;Hello World&#8221; in a web browser.<\/p>\n<p>Create a Node.js file named &#8220;myfirst.js&#8221;, and add the following code:<\/p>\n<p><span style=\"color: #ff0000;\"><strong>myfirst.js<\/strong><\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var http = require('http');\r\n\r\nhttp.createServer(function (req, res) {\r\n  res.writeHead(200, {'Content-Type': 'text\/html'});\r\n  res.end('Hello World!');\r\n}).listen(8080);<\/pre>\n<p>Save the file on your computer: C:\\Users\\<em>Your Name<\/em>\\myfirst.js<\/p>\n<p>The code tells the computer to write &#8220;Hello World!&#8221; if anyone (e.g. a web browser) tries to access your computer on port 8080.<\/p>\n<p>For now, you do not have to understand the code. It will be explained later.<\/p>\n<hr \/>\n<h2>Command Line Interface<\/h2>\n<p>Node.js files must be initiated in the &#8220;Command Line Interface&#8221; program of your computer.<\/p>\n<p>How to open the command line interface on your computer depends on the operating system. For Windows users, press the start button and look for &#8220;Command Prompt&#8221;, or simply write &#8220;cmd&#8221; in the search field.<\/p>\n<p>Navigate to the folder that contains the file &#8220;myfirst.js&#8221;, the command line interface window should look something like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">C:\\Users\\Your Name&gt;_<\/pre>\n<h2>Initiate the Node.js File<\/h2>\n<p>The file you have just created must be initiated by Node.js before any action can take place.<\/p>\n<p>Start your command line interface, write\u00a0<code class=\"w3-codespan\">node myfirst.js<\/code>\u00a0and hit enter:<\/p>\n<div class=\"w3-example\">\n<p>Initiate &#8220;myfirst.js&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">C:\\Users\\Your Name&gt;node myfirst.js<\/pre>\n<p>Now, your computer works as a server!<\/p>\n<p>If anyone tries to access your computer on port 8080, they will get a &#8220;Hello World!&#8221; message in return!<\/p>\n<p>Start your internet browser, and type in the address:\u00a0<a href=\"http:\/\/localhost:8080\/\" target=\"_blank\" rel=\"noopener\">http:\/\/localhost:8080<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Download Node.js The official Node.js website has installation instructions for Node.js:\u00a0https:\/\/nodejs.org Getting <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/node-js-getting-started\/\" title=\"Node.js &#8211; Getting Started\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4381,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[521,12],"tags":[453,525,524,523,527,522,526],"newstopic":[587],"class_list":{"0":"post-4204","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-node-js","8":"category-web-development","9":"tag-node-js","10":"tag-node-js-file-system-module","11":"tag-node-js-http-module","12":"tag-node-js-modules","13":"tag-node-js-npm","14":"tag-node-js-tutorial","15":"tag-node-js-url-module","16":"newstopic-node-js"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4204"}],"version-history":[{"count":2,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4204\/revisions"}],"predecessor-version":[{"id":4380,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4204\/revisions\/4380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4381"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4204"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}