Express Basics
Express is the most popular framework for Node.js, and it powers countless real-world sites. A framework is a set of tools that solves common problems so you do not have to. With plain Node.js, routing, request parsing, and errors are all your job. Express turns each of those into one short line, which is why most Node.js projects reach for it.
Express is built on top of the http module you already know, so nothing here is magic. You install it with npm, require it, and define routes. A route matches a web address like / or /about and decides what to send back. This lesson builds a working express server from scratch, one piece at a time.
Before you start, you need Node.js and npm installed and a folder for the project. You should also be comfortable with the callback style from earlier lessons. The setup takes two commands: npm init --yes, then npm install express. Watch the npm output, because it prints warnings when something needs attention.
A common mistake is skipping error handling. Express answers requests that match no route with a plain 404 page, but servers crash on missing input, failed database calls, and malformed data. The fix is a small catch-all handler at the end of your routes, shown in the third section. Adding it early saves hours of confusing debugging later.
Set up the project
Create a folder and run npm init --yes inside it. Then install express. This creates a node_modules folder with the express package and all its dependencies. The first install can take a few seconds while npm downloads them. When it finishes, express is ready to require.
npm init --yes
npm install express
Hello from Express
Save this file as app.js and run it with node app.js. Express creates the server with one function call. The get method sets up a route for the root address. The callback receives the request and response, and send answers with a string or with HTML. No writeHead, no end, no content type: express handles those details.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("<h1>Hello from Express</h1>");
});
app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
Handle missing routes and errors
Two lines make an Express app much friendlier. The first catches every address that matches no route and answers with a custom 404 instead of the default page. The second is an error handler that Express recognizes by the leading err argument and calls whenever any route throws. Put both after all your routes, in that order.
app.use((req, res) => {
res.status(404).send("Page not found");
});
app.use((err, req, res, next) => {
console.error(err.message);
res.status(500).send("Something went wrong");
});
Syntax
const express = require("express"); // npm package, installed above
const app = express(); // create the server
app.get("/", (req, res) => {
res.send("<h1>Hello from Express</h1>"); // send HTML back to the browser
});
app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
Express Needs app.listen, Not http.createServer
Express app is a handler — call app.listen(3000), not http.createServer(app) unless you need both.
app.listen(3000)
Good to know
- Express is the most popular Node.js framework.
- Install it with npm install express.
- app.get defines a route for a web address.
- Add a catch-all handler so unknown routes get a 404.