Skip to content

Commit 05bd393

Browse files
New blog post, /post redirects.
1 parent 25586c6 commit 05bd393

File tree

17 files changed

+315
-17
lines changed

17 files changed

+315
-17
lines changed

blog/.htaccess

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
RemoveType .shtml .html .htm .js .css .png .jpg .gif
1+
RemoveType .js .css .png .jpg .gif

blog/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ <h1 class="site-title"><a href="https://www.nodebeginner.org/blog/">The Node Beg
3131
<div class="content">
3232
<div class="posts">
3333

34+
<div class="post">
35+
<h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/">Explained: What are Node.js modules?</a></h2>
36+
<span class="post-date">Jul 5, 2017</span>
37+
38+
39+
<div class="summary">
40+
<p>When you write Node.js applications, you could actually put all your code into one huge <em>index.js</em> file, no matter
41+
how large or complex your application is. The Node.js interpreter doesn&rsquo;t care. But in terms of code organization,
42+
you would end up with a hard to understand and hard to debug mess quite quickly. So as a human being, you should care
43+
about how to structure your code. This is where modules come in. </p>
44+
<a class="read-more" href="/blog/post/nodejs-tutorial-what-are-node.js-modules/">Read more</a>
45+
</div>
46+
47+
</div>
48+
3449
<div class="post">
3550
<h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/">Node.js tutorial: Optimizing code performance using async</a></h2>
3651
<span class="post-date">Jun 6, 2017</span>

blog/index.xml

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@
66
<description>Recent content on The Node Beginner Blog</description>
77
<generator>Hugo -- gohugo.io</generator>
88
<language>en-us</language>
9-
<lastBuildDate>Tue, 06 Jun 2017 08:22:42 +0200</lastBuildDate>
9+
<lastBuildDate>Wed, 05 Jul 2017 21:52:56 +0200</lastBuildDate>
1010

1111
<atom:link href="https://www.nodebeginner.org/blog/index.xml" rel="self" type="application/rss+xml" />
1212

1313

14+
<item>
15+
<title>Explained: What are Node.js modules?</title>
16+
<link>https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/</link>
17+
<pubDate>Wed, 05 Jul 2017 21:52:56 +0200</pubDate>
18+
19+
<guid>https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/</guid>
20+
<description>&lt;p&gt;When you write Node.js applications, you could actually put all your code into one huge &lt;em&gt;index.js&lt;/em&gt; file, no matter
21+
how large or complex your application is. The Node.js interpreter doesn&amp;rsquo;t care. But in terms of code organization,
22+
you would end up with a hard to understand and hard to debug mess quite quickly. So as a human being, you should care
23+
about how to structure your code. This is where modules come in. &lt;/p&gt;</description>
24+
</item>
25+
1426
<item>
1527
<title>Node.js tutorial: Optimizing code performance using async</title>
1628
<link>https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/</link>

blog/php.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
short_open_tag=off
2+
zlib.output_compression=1
3+
zlib.output_compression_level=9
4+
auto_prepend_file=/homepages/0/d271931425/htdocs/www.nodebeginner.org/header.php
5+
auto_append_file=/homepages/0/d271931425/htdocs/www.nodebeginner.org/footer.php

blog/post/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ <h1 class="site-title"><a href="https://www.nodebeginner.org/blog/">The Node Beg
3030
<div class="content">
3131
<div class="posts">
3232

33+
<div class="post">
34+
<h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/">Explained: What are Node.js modules?</a></h2>
35+
<span class="post-date">Jul 5, 2017</span>
36+
37+
38+
<div class="summary">
39+
<p>When you write Node.js applications, you could actually put all your code into one huge <em>index.js</em> file, no matter
40+
how large or complex your application is. The Node.js interpreter doesn&rsquo;t care. But in terms of code organization,
41+
you would end up with a hard to understand and hard to debug mess quite quickly. So as a human being, you should care
42+
about how to structure your code. This is where modules come in. </p>
43+
<a class="read-more" href="/blog/post/nodejs-tutorial-what-are-node.js-modules/">Read more</a>
44+
</div>
45+
46+
</div>
47+
3348
<div class="post">
3449
<h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/">Node.js tutorial: Optimizing code performance using async</a></h2>
3550
<span class="post-date">Jun 6, 2017</span>

blog/post/index.xml

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@
66
<description>Recent content in Posts on The Node Beginner Blog</description>
77
<generator>Hugo -- gohugo.io</generator>
88
<language>en-us</language>
9-
<lastBuildDate>Tue, 06 Jun 2017 08:22:42 +0200</lastBuildDate>
9+
<lastBuildDate>Wed, 05 Jul 2017 21:52:56 +0200</lastBuildDate>
1010

1111
<atom:link href="https://www.nodebeginner.org/blog/post/index.xml" rel="self" type="application/rss+xml" />
1212

1313

14+
<item>
15+
<title>Explained: What are Node.js modules?</title>
16+
<link>https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/</link>
17+
<pubDate>Wed, 05 Jul 2017 21:52:56 +0200</pubDate>
18+
19+
<guid>https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/</guid>
20+
<description>&lt;p&gt;When you write Node.js applications, you could actually put all your code into one huge &lt;em&gt;index.js&lt;/em&gt; file, no matter
21+
how large or complex your application is. The Node.js interpreter doesn&amp;rsquo;t care. But in terms of code organization,
22+
you would end up with a hard to understand and hard to debug mess quite quickly. So as a human being, you should care
23+
about how to structure your code. This is where modules come in. &lt;/p&gt;</description>
24+
</item>
25+
1426
<item>
1527
<title>Node.js tutorial: Optimizing code performance using async</title>
1628
<link>https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/</link>

blog/post/node-v8_0_0-released/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ <h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/node-v8_0
4444
</div>
4545
</div>
4646
<div class="pagination">
47-
<a class="btn previous " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-whatwg-url-parser/"> Prev</a>
47+
48+
<a class="btn next " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-whatwg-url-parser/">Next</a>
49+
50+
4851
</div>
4952
</div>
5053
</div>

blog/post/nodejs-tutorial-optimizing-code-performance-using-async/index.html

+11-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ <h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tu
8080
</code></pre>
8181

8282
<p>Handling expensive operations asynchronously in the background, especially if they are
83-
IO-bound, is an important key to making Node.js applications perform efficiently -
83+
IO-bound, is an important key to <a href="https://www.nodebeginner.org/#passing-functions-around">making Node.js applications perform efficiently</a> -
8484
reading a large file or writing a lot of records to a database will always be a costly
8585
procedure, but handling it asynchronously at least ensures that the other parts of our
8686
application won&rsquo;t be blocked while that procedure is going on.</p>
@@ -136,7 +136,7 @@ <h2 id="executing-expensive-asynchronous-background-tasks-in-parallel">Executing
136136
</code></pre>
137137

138138
<p>This gives us a very simple &ldquo;echo&rdquo; server - if we request the URL
139-
<a href="http://localhost:8080/getUser?id=4">http://localhost:8080/getUser?id=4</a>, we receive
139+
h&#8291;ttp://localhost:8080/getUser?id=4, we receive
140140
<code>{&quot;pathname&quot;:&quot;/getUser&quot;,&quot;id&quot;:&quot;4&quot;,&quot;value&quot;:67}</code> as the response. This is good enough to
141141
give us the simulation of a <a class="internal" href="https://www.nodebeginner.org/">remote webservice API</a> to play around with.</p>
142142

@@ -309,7 +309,7 @@ <h2 id="executing-expensive-asynchronous-background-tasks-in-parallel">Executing
309309
<p><em>async</em> is a clever little module that makes managing complex control flows in our
310310
code a breeze.</p>
311311

312-
<p>After installing the module via <code>npm install [email protected]</code>, we can write our client like
312+
<p>After <a href="/blog/post/nodejs-tutorial-what-are-node.js-modules/">installing the module</a> via <code>npm install [email protected]</code>, we can write our client like
313313
this:</p>
314314

315315
<pre><code>'use strict';
@@ -381,7 +381,7 @@ <h2 id="optimizing-code-structure-with-async">Optimizing code structure with <em
381381
These are interesting even if our concern isn&rsquo;t performance optimization. Let&rsquo;s
382382
investigate them.</p>
383383

384-
<p>For these cases, let&rsquo;s remove the artificial slowness from the API server
384+
<p>For these cases, let&rsquo;s remove the artificial slowness from our <a href="Node.js API server">https://www.nodebeginner.org/#a-basic-http-server</a>
385385
by removing the <em>setTimeout</em> operation on line 19 and 22, making the server respond
386386
immediately:</p>
387387

@@ -624,8 +624,13 @@ <h2 id="optimizing-code-structure-with-async">Optimizing code structure with <em
624624
to Node.js development</a>, and both books together are <a href="https://leanpub.com/b/node-beginner-and-craftsman-bundle/">available from Leanpub for only $9.00</a>.</p>
625625
</div>
626626
</div>
627-
<div class="pagination">
628-
<a class="btn next " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-whatwg-url-parser/"> Next</a>
627+
<div class="pagination">
628+
629+
<a class="btn next " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/">Next</a>
630+
631+
632+
<a class="btn previous " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-whatwg-url-parser/">Prev</a>
633+
629634
</div>
630635
</div>
631636
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
7+
<title> Explained: What are Node.js modules? &middot; The Node Beginner Blog </title>
8+
9+
10+
<link rel="stylesheet" href="https://www.nodebeginner.org/blog/css/slim.css">
11+
<link rel="stylesheet" href="https://www.nodebeginner.org/blog/css/highlight.min.css">
12+
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Source+Code+Pro' rel='stylesheet' type='text/css'>
13+
14+
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
15+
<link rel="shortcut icon" href="/favicon.ico">
16+
17+
18+
<link href="" rel="alternate" type="application/rss+xml" title="The Node Beginner Blog" />
19+
20+
</head>
21+
22+
<body>
23+
<div class="container">
24+
<div class="header">
25+
<h1 class="site-title"><a href="https://www.nodebeginner.org/blog/">The Node Beginner Blog</a></h1>
26+
<p class="site-tagline">Node.js tutorials, how-to&#39;s, and more.</p>
27+
28+
29+
</div>
30+
<div class="content">
31+
<div class="posts">
32+
<div class="post">
33+
<h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-what-are-node.js-modules/">Explained: What are Node.js modules?</a></h2>
34+
<span class="post-date">Jul 5, 2017 </span>
35+
<div class="post-content">
36+
<p>When you write Node.js applications, you could actually put all your code into one huge <em>index.js</em> file, no matter
37+
how large or complex your application is. The Node.js interpreter doesn&rsquo;t care. But in terms of code organization,
38+
you would end up with a hard to understand and hard to debug mess quite quickly. So as a human being, you should care
39+
about how to structure your code. This is where modules come in. </p>
40+
41+
<p>You can think of Node.js modules as JavaScript libraries - a certain part of your overall codebase (for example, a
42+
collection of functions) which you want to keep together, but which you also want to keep separated from the rest of
43+
your codebase to keep things cleanly separated.</p>
44+
45+
<p>Just like we keep our socks in one drawer and our shirts in another drawer in our wardrobe - even if we combine
46+
both to create an outfit for the day - we can keep different parts of our codebase in different modules and then combine
47+
them into a coherent application.</p>
48+
49+
<h2 id="built-in-modules">Built-in Modules</h2>
50+
51+
<p>Even if we don&rsquo;t create any Node.js modules ourselves, we already have modules at our disposal because the Node.js
52+
environment provides built-in modules for us. We already encountered one of these modules in this blog when we looked at
53+
<a href="/blog/post/nodejs-tutorial-whatwg-url-parser/">how to use the built-in WHATWG URL parser</a>.</p>
54+
55+
<p>Within our own code file, we needed to use existing code which was declared elsewhere - in this case, the <code>URL</code> class
56+
which is part of the built-in Node.js module <code>url</code>:</p>
57+
58+
<pre><code>var URL = require('url').URL;
59+
</code></pre>
60+
61+
<p>The <code>require('url')</code> part is what gives us access to the code of the <code>url</code> module. Where and how this module is defined
62+
is completely opaque for us - all we need to know is its name - <code>url</code> - and the attributes it exposes, like <code>URL</code>.</p>
63+
64+
<p>Other built-in modules directly expose the attribute we need (which often are JavaScript objects). The <code>http</code> module is
65+
an example:</p>
66+
67+
<pre><code>var http = require('http');
68+
69+
http.createServer(function (req, res) {
70+
res.writeHead(200, {'Content-Type': 'text/html'});
71+
res.end('Hello World!');
72+
}).listen(8080);
73+
</code></pre>
74+
75+
<p>Here, requiring the <code>http</code> module gives us direct access to an <code>http</code> object, whose methods, like <code>createServer</code>, we can
76+
then use.</p>
77+
78+
<p><strong>The Node Beginner Book</strong> explains <a href="https://www.nodebeginner.org/">how to write a complete web server with Node.js</a>
79+
using the built-in <code>http</code> module.</p>
80+
81+
<h2 id="external-modules">External Modules</h2>
82+
83+
<p>The built-in modules which ship with Node.js allow to solve a lot of coding problems without reinventing the wheel for
84+
every new application, but what really boosts Node.js programming productivity is the huge ecosystem of open source
85+
modules provided by the Node.js community. These modules can be integrated into our codebase, too, but because they are
86+
not built-in and don&rsquo;t ship directly with each installation of Node.js, it is not enough to <code>require</code> them from our own
87+
code. We need to install the codebase containing the external module locally first, which is made very easy thanks to
88+
<em>NPM</em>, the Node Package Manager.</p>
89+
90+
<p>See this blog post which describes
91+
<a href="/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/">how to use the <em>async</em> library</a> for an
92+
example of how to use external modules.</p>
93+
94+
<p>Also, <strong>The Node Craftsman Book</strong> has a chapter on the details of
95+
<a href="https://leanpub.com/b/node-beginner-and-craftsman-bundle">working with NPM</a>.</p>
96+
97+
<h2 id="creating-your-own-modules">Creating your own modules</h2>
98+
99+
<p>Built-in and external modules are provided by others, but nothing stops you from creating your own Node.js modules.</p>
100+
101+
<p>The following example creates a module <code>myRandom</code> which provides a helper function that returns random number between 1
102+
and 10:</p>
103+
104+
<p>File <code>myRandom.js</code>:</p>
105+
106+
<pre><code>function getRandom(min, max) {
107+
return Math.random() * (max - min) + min;
108+
}
109+
110+
exports.between1and10 = function() {
111+
return getRandom(1, 10);
112+
};
113+
</code></pre>
114+
115+
<p>You would put this code into it&rsquo;s own file called <code>myRandom.js</code>. Because this file exists and provides attributes to the
116+
outer world via <code>exports</code>, another file <code>index.js</code> can use the exported functionality:</p>
117+
118+
<p>File <code>index.js</code>:</p>
119+
120+
<pre><code>var myRandom = require('./myRandom.js');
121+
122+
console.log(myRandom.between1and10());
123+
</code></pre>
124+
125+
<p>Again, <code>require</code> comes into play, making the exported attributes of the local <code>myRandom</code> module available - in this
126+
case, the <code>between1and10</code> function.</p>
127+
128+
<p>Note: While the module system allows to <em>expose</em> functionality of a module, it also allows to <em>hide</em> functionality which
129+
is not needed outside of the module, simply by not exporting the functionality via <code>export</code>. Even though the <code>index.js</code>
130+
file has required the <code>myRandom.js</code> file, it cannot access the non-exported <code>getRandom</code> function:</p>
131+
132+
<pre><code>var myRandom = require('./myRandom.js');
133+
134+
console.log(myRandom.getRandom(5, 99));
135+
</code></pre>
136+
137+
<p>will result in a <code>TypeError: myRandom.getRandom is not a function</code>.</p>
138+
139+
<p>By hiding implementation details in a module and only exposing those parts which are to be used by other parts of your
140+
codebase, you can keep your codebase well organized.</p>
141+
142+
<hr />
143+
144+
<p>Learn more about working with modules and <a href="https://www.nodebeginner.org/#a-basic-http-server">building your own web server</a>
145+
application with Node.js with <strong>The Node Beginner Book</strong> - the first part of this
146+
<a href="https://www.nodebeginner.org/">step-by-step Node.js tutorial</a> is available for free!</p>
147+
</div>
148+
</div>
149+
<div class="pagination">
150+
151+
152+
<a class="btn previous " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/">Prev</a>
153+
154+
</div>
155+
</div>
156+
</div>
157+
158+
<div class="footer">
159+
160+
<p>Copyright (c) 2017 Manuel Kiessling</p>
161+
162+
</div>
163+
164+
</div>
165+
<script src="https://www.nodebeginner.org/blog/js/slim.js"></script>
166+
<script src="https://www.nodebeginner.org/blog/js/highlight.min.js"></script>
167+
<script>
168+
hljs.initHighlightingOnLoad();
169+
</script>
170+
<script>
171+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
172+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
173+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
174+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
175+
176+
ga('create', 'UA-2127388-6', 'auto');
177+
ga('send', 'pageview');
178+
179+
</script>
180+
181+
</body>
182+
183+
</html>

blog/post/nodejs-tutorial-whatwg-url-parser/index.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,16 @@ <h2 class="post-title"><a href="https://www.nodebeginner.org/blog/post/nodejs-tu
8181
// prints 'https://www.example.com/foo?newBar=2#main'
8282
</code></pre>
8383

84-
<p>Learn more about working with URLs and building your own web server application with Node.js with <strong>The Node Beginner Book</strong> - <a href="https://www.nodebeginner.org/">the first part of this extensive tutorial</a> is available for free!</p>
84+
<p>Learn more about working with URLs and <a href="https://www.nodebeginner.org/#a-basic-http-server">building your own web server</a> application with Node.js with <strong>The Node Beginner Book</strong> - the first part of this <a href="https://www.nodebeginner.org/">extensive Node.js tutorial</a> is available for free!</p>
8585
</div>
8686
</div>
8787
<div class="pagination">
88-
<a class="btn previous " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/"> Prev</a>
89-
<a class="btn next " href="https://www.nodebeginner.org/blog/post/node-v8_0_0-released/"> Next</a>
88+
89+
<a class="btn next " href="https://www.nodebeginner.org/blog/post/nodejs-tutorial-optimizing-code-performance-using-async/">Next</a>
90+
91+
92+
<a class="btn previous " href="https://www.nodebeginner.org/blog/post/node-v8_0_0-released/">Prev</a>
93+
9094
</div>
9195
</div>
9296
</div>

blog/post/php.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
short_open_tag=off
2+
zlib.output_compression=1
3+
zlib.output_compression_level=9
4+
auto_prepend_file=/homepages/0/d271931425/htdocs/www.nodebeginner.org/header.php
5+
auto_append_file=/homepages/0/d271931425/htdocs/www.nodebeginner.org/footer.php

0 commit comments

Comments
 (0)