<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://classes.engineering.wustl.edu/cse330/index.php?action=history&amp;feed=atom&amp;title=Module5-NodeJS-New</id>
	<title>Module5-NodeJS-New - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://classes.engineering.wustl.edu/cse330/index.php?action=history&amp;feed=atom&amp;title=Module5-NodeJS-New"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;action=history"/>
	<updated>2026-11-15T19:35:16Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3457&amp;oldid=prev</id>
		<title>Todd: /* One thread? Concurrency? */</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3457&amp;oldid=prev"/>
		<updated>2026-02-26T16:32:12Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;One thread? Concurrency?&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 16:32, 26 February 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l71&quot; &gt;Line 71:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 71:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* The server remains responsive because it avoids blocking on slow I/O.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* The server remains responsive because it avoids blocking on slow I/O.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;(There are deeper details in the real Node event loop model, but the above understanding will serve you well for CSE &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;330&lt;/del&gt;.)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;(There are deeper details in the real Node event loop model, but the above understanding will serve you well for CSE &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;3300&lt;/ins&gt;.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Installing Node.js ==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Installing Node.js ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Todd</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3439&amp;oldid=prev</id>
		<title>Todd at 16:08, 25 February 2026</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3439&amp;oldid=prev"/>
		<updated>2026-02-25T16:08:19Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 16:08, 25 February 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot; &gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It is situations like these that Node.js comes to the rescue.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It is situations like these that Node.js comes to the rescue.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;'''Important correction:''' Node.js is not a &amp;quot;web framework.&amp;quot; &lt;/del&gt;Node.js is a '''JavaScript runtime''' (built on Chrome's V8 engine) that lets you run JavaScript outside the browser. Frameworks like Express run ''on top'' of Node.js. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;:contentReference[oaicite:2]{index=2}&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Node.js is a '''JavaScript runtime''' (built on Chrome's V8 engine) that lets you run JavaScript outside the browser. Frameworks like Express run ''on top'' of Node.js.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In general, JavaScript frameworks are trendy; it can be hard to keep up with the new cool thing. In fact, there's a joke website https://dayssincelastjavascriptframework.com/ that &amp;quot;tracks&amp;quot; how long it's been since a new framework has come out.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In general, JavaScript frameworks are trendy; it can be hard to keep up with the new cool thing. In fact, there's a joke website https://dayssincelastjavascriptframework.com/ that &amp;quot;tracks&amp;quot; how long it's been since a new framework has come out.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Todd</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3438&amp;oldid=prev</id>
		<title>Todd at 16:07, 25 February 2026</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3438&amp;oldid=prev"/>
		<updated>2026-02-25T16:07:42Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 16:07, 25 February 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;AJAX is a useful technology for web applications that need to pull periodic updates from your server. But what do you do when you have thousands of clients all requesting data that changes in real-time? A PHP+AJAX approach could &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;choke &lt;/del&gt;without a lot of effort on your part.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;AJAX is a useful technology for web applications that need to pull periodic updates from your server. But what do you do when you have thousands of clients all requesting data that changes in real-time? A PHP+AJAX approach could &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;struggle &lt;/ins&gt;without a lot of effort on your part.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It is situations like these that Node.js comes to the rescue.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It is situations like these that Node.js comes to the rescue.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Todd</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3437&amp;oldid=prev</id>
		<title>Todd: Created page with &quot;AJAX is a useful technology for web applications that need to pull periodic updates from your server. But what do you do when you have thousands of clients all requesting data...&quot;</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse330/index.php?title=Module5-NodeJS-New&amp;diff=3437&amp;oldid=prev"/>
		<updated>2026-02-25T16:07:20Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;AJAX is a useful technology for web applications that need to pull periodic updates from your server. But what do you do when you have thousands of clients all requesting data...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;AJAX is a useful technology for web applications that need to pull periodic updates from your server. But what do you do when you have thousands of clients all requesting data that changes in real-time? A PHP+AJAX approach could choke without a lot of effort on your part.&lt;br /&gt;
&lt;br /&gt;
It is situations like these that Node.js comes to the rescue.&lt;br /&gt;
&lt;br /&gt;
'''Important correction:''' Node.js is not a &amp;quot;web framework.&amp;quot; Node.js is a '''JavaScript runtime''' (built on Chrome's V8 engine) that lets you run JavaScript outside the browser. Frameworks like Express run ''on top'' of Node.js. :contentReference[oaicite:2]{index=2}&lt;br /&gt;
&lt;br /&gt;
In general, JavaScript frameworks are trendy; it can be hard to keep up with the new cool thing. In fact, there's a joke website https://dayssincelastjavascriptframework.com/ that &amp;quot;tracks&amp;quot; how long it's been since a new framework has come out.&lt;br /&gt;
&lt;br /&gt;
== Non-Blocking I/O ==&lt;br /&gt;
&lt;br /&gt;
You dabbled in the concept of non-blocking I/O when you implemented AJAX in the previous JavaScript module. Node.js uses this execution model at its core.&lt;br /&gt;
&lt;br /&gt;
A simple way to think about Node:&lt;br /&gt;
&lt;br /&gt;
* Node is designed to handle lots of connections efficiently.&lt;br /&gt;
* Instead of waiting for slow operations (network, file system, database) to finish, Node starts the work and continues doing other things.&lt;br /&gt;
* When the slow operation finishes, Node runs some code to handle the result.&lt;br /&gt;
&lt;br /&gt;
Historically, the core idea was the use of '''''anonymous functions''''' as ''callbacks'' for processing the results of intensive operations. Consider the pseudo-code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
// Blocking I/O Pseudo-Code&lt;br /&gt;
&lt;br /&gt;
var data = database.query(&amp;quot;select * from news&amp;quot;);&lt;br /&gt;
print data[0].title;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above pseudo-code queries a database, saves the result of the query in a variable named ''data'', and then prints out the first title in ''data''. The problem arises when multiple clients all want to access the server at the same time.&lt;br /&gt;
&lt;br /&gt;
''In a blocking I/O model, a client needs to wait until all other clients are finished before it can start its process.'' With intensive operations and thousands of clients, this can easily crash your server (or force you to provision much more hardware than you want).&lt;br /&gt;
&lt;br /&gt;
The solution is to use ''non-blocking I/O''. Consider this revised pseudo-code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
// Non-Blocking I/O Pseudo-Code&lt;br /&gt;
&lt;br /&gt;
database.query(&amp;quot;select * from news&amp;quot;, function(data){&lt;br /&gt;
	print data[0].title;&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The revised pseudo-code achieves the same end result as the first version. However, ''it allows other clients to perform tasks while this process is waiting for the database to be queried!''&lt;br /&gt;
&lt;br /&gt;
The code occurs in two parts:&lt;br /&gt;
&lt;br /&gt;
# Start the query.&lt;br /&gt;
# ''When the query is complete,'' run the callback function.&lt;br /&gt;
&lt;br /&gt;
In Node.js, callback functions are usually passed one or more parameters.&lt;br /&gt;
&lt;br /&gt;
=== Modern Node style: Promises and async/await ===&lt;br /&gt;
&lt;br /&gt;
Callbacks are still important to understand (they explain why Node &amp;quot;works&amp;quot;), but modern Node code often uses '''Promises''' and '''async/await''' because they make programs easier to read and debug. :contentReference[oaicite:3]{index=3}&lt;br /&gt;
&lt;br /&gt;
Conceptually:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
// Promise + async/await pseudo-code&lt;br /&gt;
&lt;br /&gt;
const data = await database.query(&amp;quot;select * from news&amp;quot;);&lt;br /&gt;
console.log(data[0].title);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is still non-blocking behind the scenes—it's just nicer syntax for “start work now, continue later when it finishes.”&lt;br /&gt;
&lt;br /&gt;
=== One thread? Concurrency? ===&lt;br /&gt;
&lt;br /&gt;
You will often hear that Node “runs in one thread.” The key point for this course is:&lt;br /&gt;
&lt;br /&gt;
* You can handle many concurrent connections without making a thread per client.&lt;br /&gt;
* The server remains responsive because it avoids blocking on slow I/O.&lt;br /&gt;
&lt;br /&gt;
(There are deeper details in the real Node event loop model, but the above understanding will serve you well for CSE 330.)&lt;br /&gt;
&lt;br /&gt;
== Installing Node.js ==&lt;br /&gt;
&lt;br /&gt;
We will install Node using '''nvm''' (Node Version Manager). This makes it easy to switch Node versions and consistently use the current LTS version. :contentReference[oaicite:4]{index=4}&lt;br /&gt;
&lt;br /&gt;
Run the following commands:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash&lt;br /&gt;
$ source ~/.bashrc&lt;br /&gt;
$ nvm install --lts&lt;br /&gt;
$ node -v&lt;br /&gt;
$ npm -v&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
:contentReference[oaicite:5]{index=5}&lt;br /&gt;
&lt;br /&gt;
'''Note:''' As of February 2026, Node 24 is the Active LTS line. Using `nvm install --lts` means you do not have to hardcode a specific major version in the instructions. :contentReference[oaicite:6]{index=6}&lt;br /&gt;
&lt;br /&gt;
== Your First Node.js Application: Hello World ==&lt;br /&gt;
&lt;br /&gt;
Let's try the classic HTTP server example.&lt;br /&gt;
&lt;br /&gt;
=== Modern version (ES Modules) ===&lt;br /&gt;
&lt;br /&gt;
Create a file named `hello.js`:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
import http from &amp;quot;node:http&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
http.createServer((req, res) =&amp;gt; {&lt;br /&gt;
	res.writeHead(200, { &amp;quot;Content-Type&amp;quot;: &amp;quot;text/plain&amp;quot; });&lt;br /&gt;
	res.end(&amp;quot;Hello World\n&amp;quot;);&lt;br /&gt;
}).listen(3456);&lt;br /&gt;
&lt;br /&gt;
console.log(&amp;quot;Server running at http://localhost:3456/&amp;quot;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ node hello.js&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Note:''' If you're running the code from your EC2 instance, you will need to open port 3456.&lt;br /&gt;
&lt;br /&gt;
=== Enabling ES Modules (important!) ===&lt;br /&gt;
&lt;br /&gt;
Modern Node supports both the older '''CommonJS''' module system (require) and the newer JavaScript standard '''ES Modules''' (import/export). ES Modules are the direction modern JavaScript has moved (and they match browser JavaScript). :contentReference[oaicite:7]{index=7}&lt;br /&gt;
&lt;br /&gt;
To tell Node to treat `.js` files as ES Modules, add this to `package.json` in your project directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;type&amp;quot;: &amp;quot;module&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
:contentReference[oaicite:8]{index=8}&lt;br /&gt;
&lt;br /&gt;
If you do not do this, Node may treat `.js` files as CommonJS by default, and `import ...` may fail.&lt;br /&gt;
&lt;br /&gt;
=== Walkthrough of the Hello World example ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;import http from &amp;quot;node:http&amp;quot;;&amp;lt;/code&amp;gt; enables us to use Node's built-in HTTP server library. Unlike PHP, Node does not automatically load lots of server features for you. You explicitly import what you need.&lt;br /&gt;
* &amp;lt;code&amp;gt;http.createServer((req, res) =&amp;amp;gt; { ... }).listen(3456);&amp;lt;/code&amp;gt; listens for HTTP connections on port 3456. The callback function runs whenever a connection is made.&lt;br /&gt;
* &amp;lt;code&amp;gt;res.writeHead(200, { &amp;quot;Content-Type&amp;quot;: &amp;quot;text/plain&amp;quot; });&amp;lt;/code&amp;gt; sets the HTTP response code and headers.&lt;br /&gt;
* &amp;lt;code&amp;gt;res.end(&amp;quot;Hello World\n&amp;quot;);&amp;lt;/code&amp;gt; writes the response body and ends the response.&lt;br /&gt;
&lt;br /&gt;
Notice the use of non-blocking behavior even in this simple example: ''when a connection is made'', the callback runs with the parameters associated with that connection.&lt;br /&gt;
&lt;br /&gt;
== A Static File Server Using Node.js ==&lt;br /&gt;
&lt;br /&gt;
Node.js does not automatically serve up static files in the way that Apache/PHP does. We need to either:&lt;br /&gt;
&lt;br /&gt;
* use a framework like Express (common in industry), or&lt;br /&gt;
* write a simple static file server ourselves (great for understanding how servers work)&lt;br /&gt;
&lt;br /&gt;
We'll do the second option here.&lt;br /&gt;
&lt;br /&gt;
=== Installing Packages ===&lt;br /&gt;
&lt;br /&gt;
In order for the example code below to work, you need the '''mime''' package (it helps map filenames to Content-Type headers). The `mime` package is actively maintained. :contentReference[oaicite:9]{index=9}&lt;br /&gt;
&lt;br /&gt;
In the directory where you will save your Node.js file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ npm init -y&lt;br /&gt;
$ npm install mime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''npm'' is the package manager for Node.js. It installs dependencies locally (inside `node_modules`) by default, which is a good thing: it lets different projects use different versions of the same dependency.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' When using npm to install local dependencies, '''do not use sudo'''. It can break file permissions in your project directory.&lt;br /&gt;
&lt;br /&gt;
=== Static Fileserver Code (Modern ESM + async/await) ===&lt;br /&gt;
&lt;br /&gt;
The following code makes an HTTP server on port 3456 that serves all static files inside &amp;lt;STATIC DIRECTORY NAME&amp;gt; relative to app.js.&lt;br /&gt;
&lt;br /&gt;
'''Make an attempt to understand this code.''' It's important that you are comfortable with the non-blocking nature, as well as how we handle file I/O.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
import http from &amp;quot;node:http&amp;quot;;&lt;br /&gt;
import path from &amp;quot;node:path&amp;quot;;&lt;br /&gt;
import { readFile } from &amp;quot;node:fs/promises&amp;quot;;&lt;br /&gt;
import { existsSync } from &amp;quot;node:fs&amp;quot;;&lt;br /&gt;
import mime from &amp;quot;mime&amp;quot;;&lt;br /&gt;
import { fileURLToPath } from &amp;quot;node:url&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
const __filename = fileURLToPath(import.meta.url);&lt;br /&gt;
const __dirname = path.dirname(__filename);&lt;br /&gt;
&lt;br /&gt;
const STATIC_DIR = &amp;quot;&amp;lt;STATIC DIRECTORY NAME&amp;gt;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
const app = http.createServer(async (req, resp) =&amp;gt; {&lt;br /&gt;
	try {&lt;br /&gt;
		// Parse the path the browser requested&lt;br /&gt;
		const urlPath = new URL(req.url, &amp;quot;http://localhost&amp;quot;).pathname;&lt;br /&gt;
		const filename = path.join(__dirname, STATIC_DIR, decodeURIComponent(urlPath));&lt;br /&gt;
&lt;br /&gt;
		// If file doesn't exist, return 404&lt;br /&gt;
		if (!existsSync(filename)) {&lt;br /&gt;
			resp.writeHead(404, { &amp;quot;Content-Type&amp;quot;: &amp;quot;text/plain&amp;quot; });&lt;br /&gt;
			resp.end(&amp;quot;Requested file not found: &amp;quot; + filename);&lt;br /&gt;
			return;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Read the file (non-blocking I/O via promises)&lt;br /&gt;
		const data = await readFile(filename);&lt;br /&gt;
&lt;br /&gt;
		// Determine content type&lt;br /&gt;
		const mimetype = mime.getType(filename) || &amp;quot;application/octet-stream&amp;quot;;&lt;br /&gt;
		resp.writeHead(200, { &amp;quot;Content-Type&amp;quot;: mimetype });&lt;br /&gt;
		resp.end(data);&lt;br /&gt;
	} catch (err) {&lt;br /&gt;
		// File exists but isn't readable or some other error occurred&lt;br /&gt;
		resp.writeHead(500, { &amp;quot;Content-Type&amp;quot;: &amp;quot;text/plain&amp;quot; });&lt;br /&gt;
		resp.end(&amp;quot;Internal server error: could not read file&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.listen(3456);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== A few important notes about the modern version ====&lt;br /&gt;
&lt;br /&gt;
* We use &amp;lt;code&amp;gt;node:fs/promises&amp;lt;/code&amp;gt; so we can write file I/O using async/await.&lt;br /&gt;
* In ES Modules, &amp;lt;code&amp;gt;__dirname&amp;lt;/code&amp;gt; is not automatically defined, so we compute it using &amp;lt;code&amp;gt;import.meta.url&amp;lt;/code&amp;gt;.&lt;br /&gt;
* This server is intentionally simple for instructional purposes.&lt;br /&gt;
&lt;br /&gt;
'''Security note:''' A production static server must protect against directory traversal attacks (paths like &amp;lt;code&amp;gt;../&amp;lt;/code&amp;gt;). For this course example, we focus on learning the mechanics first.&lt;br /&gt;
&lt;br /&gt;
=== Port 3456 ===&lt;br /&gt;
&lt;br /&gt;
We are running our server on port 3456 instead of port 80. We therefore need to open our Node.js application like this:&lt;br /&gt;
&lt;br /&gt;
http://ec2-xx-xx-xx-xx.compute-1.amazonaws.com:3456/path.extension&lt;br /&gt;
&lt;br /&gt;
However, this won't work the first time. ''Why?''&lt;br /&gt;
&lt;br /&gt;
We need to open port 3456 on our Amazon EC2 instance. Remember when you opened port 80 back in Module 2? We need to do the same procedure again now.&lt;br /&gt;
&lt;br /&gt;
# Log into your EC2 management console&lt;br /&gt;
# Go to &amp;quot;Security Groups&amp;quot;&lt;br /&gt;
# Select your security group from Module 2&lt;br /&gt;
# Under &amp;quot;Inbound&amp;quot;, create a custom TCP rule for port 3456, and apply your changes.&lt;br /&gt;
&lt;br /&gt;
== Node.js Security ==&lt;br /&gt;
&lt;br /&gt;
Just because we're writing in JavaScript doesn't mean we can forget about the security practices you've been learning in CSE 330. XSS is still a problem, as is the use of ''eval'', CSRF attacks, and so on.&lt;br /&gt;
&lt;br /&gt;
Two links with Node-specific security discussions:&lt;br /&gt;
&lt;br /&gt;
# Book from PragProg ($24, 2015): https://pragprog.com/book/kdnodesec/secure-your-node-js-web-application&lt;br /&gt;
# Slideshow from a Switzerland Conference on Application Security (2012): http://www.slideshare.net/ASF-WS/asfws-2012-nodejs-security-old-vulnerabilities-in-new-dresses-par-sven-vetsch&lt;br /&gt;
&lt;br /&gt;
== Node.js Conventions ==&lt;br /&gt;
&lt;br /&gt;
There ought to be a standard way to save metadata about your app, right? Node projects use a &amp;lt;code&amp;gt;package.json&amp;lt;/code&amp;gt; file for this.&lt;br /&gt;
&lt;br /&gt;
To create one quickly:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ npm init -y&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
:contentReference[oaicite:10]{index=10}&lt;br /&gt;
&lt;br /&gt;
One nice feature of &amp;lt;code&amp;gt;package.json&amp;lt;/code&amp;gt; is that it automatically handles dependencies. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;dependencies&amp;quot;: {&lt;br /&gt;
  &amp;quot;mime&amp;quot;: &amp;quot;^4.1.0&amp;quot;,&lt;br /&gt;
  &amp;quot;socket.io&amp;quot;: &amp;quot;^4.8.0&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then when you run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ npm install&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
npm installs the dependencies for you.&lt;br /&gt;
&lt;br /&gt;
'''Tip:''' Modern npm automatically saves dependencies when you run &amp;lt;code&amp;gt;npm install package-name&amp;lt;/code&amp;gt;. The older &amp;lt;code&amp;gt;--save&amp;lt;/code&amp;gt; flag is no longer necessary.&lt;br /&gt;
&lt;br /&gt;
[[Category:Module 7]]&lt;/div&gt;</summary>
		<author><name>Todd</name></author>
		
	</entry>
</feed>