Framework cheatsheet
Express 5 Cheatsheet
Build HTTP servers and APIs with Express 5 on Node.js.
Setup
- Install Express — Add Express to a Node.js project.
- Hello world server — The smallest Express app. It listens on port 3000.
- Run with auto-restart — Restart the server when files change, using Node's built-in watcher.
- Load environment variables — Read PORT and other settings from a .env file.
Routing
- Read a route parameter — Capture part of the URL as a parameter.
- Read the query string — Read values such as ?page=2.
- Handle a JSON body — Parse JSON request bodies, then read req.body.
- Group routes in a router — Put related routes in a router and mount it under a prefix.
- Send a status and JSON — Set the status code and send a JSON response.
- Redirect — Redirect the client to another URL.
Middleware
- Write middleware — Run code on every request, then call next() to continue.
- Serve static files — Serve files from the public folder.
- Enable CORS — Allow browsers on other origins to call your API.
- Set security headers — Add common protective HTTP headers.
- Log requests — Log each request with the morgan middleware.
- Trust a reverse proxy — Read the client's real IP and protocol behind a proxy such as Nginx.
Error handling
- Handle errors in async routes — In Express 5, a rejected promise or thrown error in an async handler is passed to the error handler automatically.
- Add an error handler — Define it last. It takes four arguments.
- Handle 404s — Add a catch-all after your routes.
Related cheatsheets
- Angular 22 cheatsheet — Angular CLI commands and standalone component, signals, template, routing, HTTP and forms snippets.
- Astro cheatsheet — Build fast content sites with Astro: components, layouts, routing, content collections, islands, endpoints and deployment.
- Django 6 cheatsheet — Create projects, run migrations, test and deploy Django apps, plus the ORM queries you use every day.
- .NET and ASP.NET Core 10 cheatsheet — The dotnet CLI for creating, building, testing and publishing projects, plus common ASP.NET Core and EF Core code.
- FastAPI 0.115 cheatsheet — Install, run, test and deploy FastAPI apps from the command line.
- Flask 3 cheatsheet — Create, run, test and deploy Flask apps, with routes, templates, blueprints, sessions and database snippets.