{"id":252,"date":"2020-06-01T10:40:47","date_gmt":"2020-06-01T01:40:47","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=252"},"modified":"2020-06-01T10:40:47","modified_gmt":"2020-06-01T01:40:47","slug":"nodejs-book-chapter-1","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=252","title":{"rendered":"Nodejs Book: Chapter 1"},"content":{"rendered":"\n<p>Nodejs has a deceptively short source code for the standard &#8220;hello, world&#8221;<br>version of an http server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ File: hello.sj\n\"use strict\";\n\nconst http = require(\"http\");\nconst server = http.createServer();\nserver.on(\"request\", handleRequest);\nserver.listen(8080, handleListen);\n\nfunction handleRequest(request, response) {\n    console.log(\"Request url: %s\", request.url);\n    response.writeHead(200, {\"Content-Type\" : \"text\/plain\"});\n    response.end(\"Hello, World!\");\n}\n\nfunction handleListen( ){\n    console.log(\"Server is listening on port 8080\");\n}<\/code><\/pre>\n\n\n\n<p>We can run this server with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ node hello.js\nServer is listening on port 8080<\/code><\/pre>\n\n\n\n<p>In which case the program in the console will simply stop without terminating. This is because we told our process to listen on port 8080 for any http requests, and have not given the process any specific commands to terminate. Since our web browser can send http requests, we can open up our browser and attempt to communicate with our Nodejs web server. If we type<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"797\" src=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_01-1024x797.jpg\" alt=\"\" class=\"wp-image-253\" srcset=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_01-1024x797.jpg 1024w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_01-300x234.jpg 300w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_01-768x598.jpg 768w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_01.jpg 1148w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>And in our Nodejs console we should see the output of<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Request url: \/\nRequest url: \/favicon.ico<\/code><\/pre>\n\n\n\n<p>Which is somewhat curious but easily explainable. For the &#8220;\/&#8221;, we simply sent a request to <code>http:\/\/192.168.1.16:8080<\/code> from the browser, without appending anything onto the end. The browser automatically adds the slash at the end, representing the root directory. The &#8220;\/favicon.ico&#8221; is another browser quirk. Most web sites will have an icon which is displayed on the tab of that page. This icon is referred to as a &#8220;favicon&#8221;, and the browser will automatically send a request to the web server to see if it exits. In this case it doesn&#8217;t and nothing is displayed.<\/p>\n\n\n\n<p>That alone is simple enough, but things become more confusing when we try adding more text on to the end of our url.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"789\" src=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_02-1024x789.jpg\" alt=\"\" class=\"wp-image-254\" srcset=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_02-1024x789.jpg 1024w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_02-300x231.jpg 300w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_02-768x592.jpg 768w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2020\/06\/fig_02.jpg 1152w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We can try <code>http:\/\/192.168.1.16:8080\/aaaaaaaa<\/code> or any other text onto the end of the url we request and there will be no change. For any and all requests our server will simply respond with &#8220;Hello, World!&#8221;. While this may seem a little baffling at first, because with other web servers, if you put in a random set of letters or characters into the request, you would expect to get a 404 or some other error to the effect.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function handleRequest(request, response) {\n    console.log(\"Request url: %s\", request.url);\n    response.writeHead(200, {\"Content-Type\" : \"text\/plain\"});\n    response.end(\"Hello, World!\");\n}<\/code><\/pre>\n\n\n\n<p>If we take another look at out <code>handleRequest<\/code> function the response quickly<br>becomes apparent. For all requests, regardless of the context, our server is<br>simply programmed to return the text &#8220;Hello, World!&#8221; for all requests. So how can we program our Nodejs server to do something more functional? We will continue in the next chapter we will be expand our basic text server into a basic file server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This this chapter, we go over the basics of the deceptively simple &#8220;hello world&#8221; server in Nodejs. And go over some of the concepts into why the same message will be returned to the user no matter what url is requested.<\/p>\n","protected":false},"author":1,"featured_media":249,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/252"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=252"}],"version-history":[{"count":2,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions\/256"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/media\/249"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}