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 serverRoutes
| What | How |
|---|---|
| Read | app.get("/api/users/:id", handler) |
| Create | app.post("/api/users", handler) |
| Path parameter | req.params.id |
| Body | app.use(express.json()); req.body |
| Response | res.status(201).json(data) |
The chain
| What | How |
|---|---|
| Middleware | (req, res, next) => { …; next(); } |
| Errors | app.use((err, req, res, next) => …) |
| Router | app.use("/api/users", usersRouter) |
| Protect a route | app.delete("/api/x/:id", requireUser, requireRole("admin"), h) |
Tests and production
| What | How |
|---|---|
| Request test | await request(app).get("/api/books/1").expect(200) |
| Security headers | app.use(helmet()) |
| Limit | rateLimit({ windowMs: 15 * 60 * 1000, limit: 10 }) |
| Behind a proxy | app.set("trust proxy", 1) |