Theory
JavaScript, now on the server
FestConnect's front end is built. But the front end cannot store data or enforce rules on its own; it needs a backend, a program running on a server that the front end talks to. This unit builds that backend with Node.js and Express.
Node.js lets you run JavaScript on the server (not just in the browser), so you write your whole stack in one language. Express is the framework that makes building a web API on Node clean and quick. You met Node basics before; here you build a real REST API. This lesson sets it up.
Theory
Node, Express, and REST
Node.js is a runtime that executes JavaScript outside the browser. After installing it (version 20), you run a file with node app.js, and it can act as a web server.
Express is a small, widely used framework on top of Node. It handles the fiddly parts of HTTP and lets you define your API in a few readable lines. You will build a RESTful backend: an API style where URLs represent resources (like /events) and HTTP methods represent actions (GET to read, POST to create). Express makes expressing these routes natural, which is why it is the default choice for Node APIs.
Practical
A minimal Express server
// install first: npm install express
const express = require('express');
const app = express();
app.use(express.json()); // parse JSON request bodies
app.get('/api/events', (req, res) => {
res.json([{ name: 'Robotics Workshop' }]); // respond with JSON
});
app.listen(3000, () => console.log('API running on port 3000'));This example runs in Gri-Learn on the web, where you can edit it and see the output.
Formula
nodemon: no more manual restarts
Normally, every time you change server code you must stop and restart node app.js to see the effect, tedious and easy to forget. nodemon fixes this: it watches your files and automatically restarts the server whenever you save.
You install it as a development tool (npm install --save-dev nodemon) and run nodemon app.js instead of node app.js. During development this small tool saves you countless manual restarts and keeps your edit-run loop fast. It is a dev convenience only; in production you run with plain node.
Theory
Structuring the project
A one-file server is fine for a demo, but FestConnect's backend will grow, so you structure it into folders by responsibility. A common layout:
- routes/: define which URLs exist and which handler each maps to.
- controllers/: the functions that handle each route's logic.
- models/: the data shapes and database access.
Separating these keeps the codebase navigable as it grows, the same separation-of-concerns idea you have met throughout. Your entry file (app.js) wires them together and starts the server.
Quiz
What is the role of Express.js in a Node backend?
- It is a database that stores the events
- It is a lightweight web framework on top of Node that makes building a RESTful API simple (routes, requests, responses)
- It runs JavaScript in the browser
- It replaces Node.js entirely
Show the answer
It is a lightweight web framework on top of Node that makes building a RESTful API simple (routes, requests, responses)
Express is a minimal web framework that sits on top of Node.js and makes building a web/REST API straightforward: you define routes, read requests, and send responses in a few clean lines. Option A is wrong: Express is not a database; FestConnect uses MongoDB for storage (a later lesson). Option C describes browser JavaScript, but Node (and Express) run JavaScript on the SERVER, not in the browser. Option D is wrong: Express does not replace Node; it runs ON Node, using Node's runtime to handle HTTP more conveniently. Node is the runtime; Express is the framework that makes an API easy to write.
Think first
Why use Node for the backend when the front end is already Angular?
You could write a backend in Python, Java, or PHP. Why does full-stack JavaScript with Node appeal? Then tap.
Show the answer
The biggest draw is ONE LANGUAGE across the whole stack: with Node, both your front end (Angular/TypeScript, which is JavaScript's typed cousin) and your back end are written in JavaScript, so you switch contexts less, share mental models and even some code (like validation logic or data shapes), and your team needs fewer separate skill sets. That coherence speeds development and reduces bugs that come from translating between languages. Node also has specific strengths for web backends: it is EVENT-DRIVEN and non-blocking, meaning it handles many simultaneous connections efficiently (great for APIs serving lots of users making quick requests), and it has an enormous package ecosystem (npm) with a library for almost everything, including Express itself. JSON, the lingua franca of web APIs, is native to JavaScript, so moving data between front end, back end, and database (especially a JSON-friendly database like MongoDB) is frictionless. None of this makes Python, Java, or PHP wrong, each has its own excellent backends, but for a JavaScript-heavy, API-driven, real-time-friendly app like FestConnect, full-stack JavaScript with Node and Express is a natural, productive fit. And learning it means your Angular skills and your backend skills reinforce each other. One language, one mental model, end to end.
Summary
Key takeaways
- The backend runs on Node.js, which executes JavaScript on the server (run files with node app.js).
- Express.js is a lightweight framework on top of Node that makes building a RESTful API simple.
- REST style: URLs represent resources (like /api/events) and HTTP methods represent actions (GET, POST).
- A minimal server: create express(), define routes, and app.listen on a port.
- nodemon auto-restarts the server when files change, speeding the development loop (dev-only).
- Structure the project into routes, controllers, and models folders as it grows (separation of concerns).
- Memory hook: Node runs JS on the server, Express makes the API, nodemon reloads it.