One page

Express cheat sheet

What to keep at hand. If a line is not clear, go back to the matching lesson in the course.

Open the course: Express: an API server

Routes

WhatHow
Readapp.get("/api/users/:id", handler)
Createapp.post("/api/users", handler)
Path parameterreq.params.id
Bodyapp.use(express.json()); req.body
Responseres.status(201).json(data)

The chain

WhatHow
Middleware(req, res, next) => { …; next(); }
Errorsapp.use((err, req, res, next) => …)
Routerapp.use("/api/users", usersRouter)
Protect a routeapp.delete("/api/x/:id", requireUser, requireRole("admin"), h)

Tests and production

WhatHow
Request testawait request(app).get("/api/books/1").expect(200)
Security headersapp.use(helmet())
LimitrateLimit({ windowMs: 15 * 60 * 1000, limit: 10 })
Behind a proxyapp.set("trust proxy", 1)

Other cheat sheets