Swipe left or tap Next ▶ to turn the page.
Select any text to highlight it or add a note. Tap a diagram to enlarge it.
Node.js and Express: Modules, npm, the Event Loop, fs/path and Middleware
- ✔ Explain what Node.js is and why it handles many users with one thread
- ✔ Use CommonJS and ES modules, npm, package.json and semantic versions correctly
- ✔ Predict the output order of timers, promises, nextTick and setImmediate
- ✔ Build an Express server with routes, middleware and error handling
What Node.js really is
Nearly every full-stack round starts with "What is Node.js?", and a clear two-line answer sets the tone for the whole interview.
Node.js is a JavaScript runtime: it runs JavaScript outside the browser, on a server or your laptop. It is not a language and not a framework; it is an engine plus a toolbox for reading files, opening network ports and talking to databases.
Node.js is built from two main parts:
- V8, the JavaScript engine from Google Chrome. V8 compiles your JavaScript into fast machine code.
- libuv, a C library that gives Node.js its event loop and a small pool of background threads for slow work such as file reads, DNS lookups and compression.
On top sit the core modules and your own code.
One thread, many users
Your JavaScript runs on one main thread. Most web work is waiting: for the database, a file or another API. Node.js hands the slow job to the operating system or libuv, moves on to the next request, and runs your callback when the result is back. This is non-blocking, event-driven I/O.
Think of one waiter at a busy Chennai restaurant. He passes table 1's order to the kitchen and moves to table 2 instead of standing at the stove. When the bell rings, he delivers the dish.
Node.js suits APIs, real-time apps (chat, live scores) and build tools. It struggles with heavy CPU work on the main thread.
One request runs a loop that adds numbers for 3 seconds on the main thread.
While that loop runs, the event loop cannot pick up any other callback.
So every other user waits those 3 seconds too, even for a tiny request.
Answer: CPU-heavy work blocks all users; move it to a worker thread or a separate service.
Running Node.js and its global objects
Knowing how to run a script and read process is the first practical skill an interviewer checks when you share your screen.
After installing Node.js you get two commands: node and npm. Check them with node -v and npm -v.
Run code in the REPL (type node), from a file (node app.js) or inline (node -e "...").
Globals you get without requiring anything
| Global | What it gives you |
|---|---|
console |
log, error, table, time/timeEnd |
process |
argv, env, exit(), cwd(), pid, nextTick() |
__dirname, __filename |
Folder and full path of the current file (CommonJS only) |
setTimeout, setInterval, setImmediate |
Timers |
Buffer |
Raw binary data |
There is no window and no document in Node.js, because there is no browser page. The global object is called global, and the standard name that works everywhere is globalThis.
Reading command-line arguments
process.argv is an array. Index 0 is the path to the node program, index 1 is the path to your script, and your own arguments start at index 2.
const argv = ["/usr/bin/node", "/home/arjun/greet.js", "Arjun", "Pune"];
const [, , name, city] = argv;
console.log(`Hello ${name} from ${city}`);
console.log(argv.slice(2).length);
Hello Arjun from Pune
2
In a real run, node greet.js Arjun Pune fills process.argv the same way.
You run
PORT=4000 node server.js.Inside the code,
process.env.PORTis the string"4000", not a number.Write
const port = Number(process.env.PORT) || 3000;Answer: port is 4000; if PORT were missing, it would fall back to 3000.
CommonJS modules: require and module.exports
Most Node.js codebases and interview snippets still use require, so you must know exactly what it returns.
A module is just a file. Each file has its own private scope, so a variable you declare in cart.js does not leak into app.js. To share something, you export it; to use it, you require it.
Exporting and importing
// gst.js
const RATE = 0.18;
function withGst(amount) {
return amount + amount * RATE;
}
module.exports = { withGst, RATE };
// app.js
const { withGst } = require("./gst");
console.log(withGst(1000)); // 1180
require returns the other file's module.exports. ./ means a local file; without it, Node.js looks for a core module or a package in node_modules.
exports versus module.exports
exports is a shortcut pointing to the same object as module.exports. Adding properties works; reassigning exports breaks the link.
const module1 = { exports: {} };
let exports1 = module1.exports;
exports1.a = 1;
exports1 = { b: 2 };
console.log(module1.exports);
{ a: 1 }
This copies what Node.js does internally: require would return { a: 1 }.
Modules are cached
The first require("./db") runs the file; later calls return the same object without re-running it, so one DB connection is shared app-wide.
counter.jscontainsconsole.log("loaded"); module.exports = { n: 0 };app.jscallsconst a = require("./counter"); const b = require("./counter"); a.n++;"loaded" prints once, because the second call hits the cache.
aandbare the same object, sob.nis also 1.Answer: "loaded" prints once and b.n is 1.
Keep going with the full course
Day 1 has 26 pages. You have read the first 3.
See what's inside →