Contents
Express
A minimalist web framework for Node.js: routes, middleware, templates and REST APIs.
1.Getting started
- What Express isWhy you need Express and how it is more convenient than the bare node:http module.
- Installation and the first serverCreating a project, installing Express, starting the server with app.listen and node --watch.
- Project structureHow to lay out an Express application in folders: app.js, server.js, routes, controllers, views and public.
2.Routes, request and response
- Routes and HTTP methodsHow to connect an address and an HTTP method to a handler function in Express.
- Route parameters and the query stringHow to read part of the address via req.params and the values after the question mark via req.query.
- The request objectWhat req contains: method, address, parameters, headers, request body and the client IP address.
- The response objectHow to answer the client: res.send, res.json, res.status, res.redirect and other response methods.
- Router and routes in separate filesHow to move routes out of app.js into routes/ files with express.Router, and handlers into controllers.
3.Middleware
- What middleware isMiddleware in Express: how to write your own, what next() does and why order matters.
- Built-in middlewareexpress.json and express.urlencoded: how Express reads a request body with JSON and HTML form data.
- Static filesHow to serve CSS, images and scripts from the public folder with express.static.
- The 404 page and error handlingHow to show your own not-found page and catch errors in a single error handler.
- Async handlersHow to write handlers with async/await and how Express 5 passes their errors to the error handler by itself.
4.Pages and forms
- EJS templatesHow to build HTML pages from EJS templates: setup, res.render, printing data, conditions and loops.
- Shared page partsHow to move the site header and footer into separate templates and insert them with include.
- Forms and redirect after POSTA form for adding a post: showing the form, reading data from req.body and redirecting after submit.
- Validating form dataManual field checks and schema validation with the Zod library, showing errors next to the form.
- File uploadsHow to accept an image from a form with multer: storage setup, size and file type limits.
5.Data and users
- An SQLite databaseHow to store posts and comments in SQLite with better-sqlite3: connection, tables, a model and queries.
- Sessions and flash messagesHow to keep visitor data between requests with express-session and show one-time messages.
- User login and logoutRegistration with password hashing via bcrypt, login and logout through the session, protecting routes.
6.JSON API
- A REST API for postsThe blog's JSON API in Express: reading, creating, updating and deleting posts following REST rules.
- API authentication with JWTHow to give a client a JWT on login and check it in middleware for protected API addresses.
- CORSWhy the browser blocks API requests from another site and how to allow them with the cors package.
7.Getting ready for production
- Configuration and environment variablesHow to keep the port, secrets and database path in a .env file and read them via process.env.
- Request logging with morganHow to log every request to the server with morgan: formats, writing to a file and logging errors only.
- Security basicsProtective helmet headers, request rate limiting, input validation and secure cookies.
- Testing with supertestAutomated tests for an Express application: the built-in node:test and supertest for requests without starting a server.
- Deploying to a serverRunning the Express blog on a server: production mode, the pm2 process manager and an nginx reverse proxy.