Art of Node - Deployment & Usage Guide
A practical guide for running the Node.js callback and async examples from the Art of Node tutorial.
1. Prerequisites
- Node.js (v0.10.x or higher, any modern LTS version recommended)
- Git (for cloning the repository)
- Text editor (VS Code, Vim, Nano, or any plain text editor)
Verify Node.js installation:
node --version
npm --version
2. Installation
Clone the repository and navigate to the code examples:
git clone https://github.com/max-mapper/art-of-node.git
cd art-of-node/code
Create the required data file that the examples read from:
echo "1" > number.txt
3. Configuration
The examples require a number.txt file in the code/ directory containing an integer.
File structure:
art-of-node/
└── code/
├── 2.js
├── 3.js
├── 4.js
└── number.txt # You must create this
Contents of number.txt:
42
(Any integer value works; the scripts increment this number)
4. Build & Run
No build step is required. Run individual examples directly with Node.js:
Example 2: Async Problem (Undefined Output)
node 2.js
Expected output: undefined
Purpose: Demonstrates that console.log executes before the async file read completes.
Example 3: Callback Solution
node 3.js
Expected output: 2 (or whatever number you put in number.txt + 1)
Purpose: Shows how to use callbacks to ensure code executes after async operations complete.
Example 4: Callback Chaining
node 4.js
Expected output:
done with one, on to the next
done with one, on to the next
done with one, on to the next
Purpose: Demonstrates sequential async operations using nested callbacks (callback hell pattern).
5. Deployment
This is an educational repository designed for local learning. Deployment options depend on your use case:
Option A: Static Site (for the README/guide)
Deploy the markdown documentation to GitHub Pages or Netlify:
# GitHub Pages is automatic for this repo
# Just enable Pages in repository settings
Option B: Cloud IDE (for running examples)
Import into Glitch or CodeSandbox for browser-based execution:
- Go to glitch.com
- Import from GitHub:
max-mapper/art-of-node - Run
node code/3.jsin the Glitch terminal
Option C: Containerized (if extending)
Create a Dockerfile for production deployment:
FROM node:18-alpine
WORKDIR /app
COPY code/ ./code/
CMD ["node", "code/3.js"]
6. Troubleshooting
Error: ENOENT: no such file or directory, open './number.txt'
Cause: The number.txt file doesn't exist in the code/ directory.
Solution:
cd code
echo "1" > number.txt
Output shows undefined in example 2
Cause: This is expected behavior. The console.log executes before the async fs.readFile completes.
Solution: No fix needed. This demonstrates the problem that callbacks solve (see example 3).
Error: parseInt is not a function or NaN output
Cause: number.txt contains non-numeric data or is empty.
Solution: Ensure number.txt contains only a valid integer (e.g., 42).
Callback hell confusion in example 4
Issue: Deeply nested callbacks are hard to read.
Solution: This example intentionally demonstrates the "callback hell" anti-pattern. For production code, use Promises, async/await, or control flow libraries like async.js.