Module 41: Node.js Fundamentals
Modules in a backend project
🏠The everyday picture
A hospital with departments: reception, records, pharmacy, wards. Each has a clear job and talks to the others in agreed ways.
📘What it really is
-
Everything from Module 29 applies: ES modules,
import/export, one subject per file, dependencies pointing one way. -
A typical structure for an API (Module 43 explains each layer):
enquiries-api/
src/
server.ts starts the server
app.ts sets up the app and routes
routes/ addresses and methods
services/ business rules
data/ storage
config.ts settings
tests/
package.json
tsconfig.json
.env.example
-
Running TypeScript in development: Node.js can run
.tsfiles directly (Module 27);node --watch src/server.tsrestarts on every save. Runtsc --noEmitto check types. -
For production, many teams compile to JavaScript with
tsc(intodist/) and run that, or use a bundler. Either way, check types in CI (Level 6). -
Scripts:
"scripts": {
"dev": "node --watch src/server.ts",
"check": "tsc --noEmit",
"test": "node --test \"tests/**/*.test.ts\""
}
🖱️Try it
Create the folder structure above with a tsconfig.json (strict, with the settings from Modules 27 and 29). In
server.ts, print "Server starting". Run npm run dev, change the message, save, and watch it restart.
✅Quick check
What does node --watch do?
Show the answer
Restarts the program automatically whenever you save a file.
Running .ts files with Node.js works. Why still run tsc --noEmit?
Show the answer
Node removes types without checking them; tsc actually checks.
