Well-structured backends separate concerns: routers define URLs, controllers translate HTTP to application calls, services own data access, and middleware handles cross-cutting behavior (validation, 404, errors). Study these patterns to keep files small, testable, and aligned with REST conventions.

How to define HTTP routes on a router — Basic

Map GET, POST, PUT, and DELETE handlers to paths using router methods or router.route().
Learn: REST maps actions to HTTP verbs: GET reads, POST creates, PUT/PATCH updates, DELETE removes. Use `router.route(path)` to chain verbs on the same path. Handlers should delegate to controllers, not contain SQL.

Supplementary examples

CRUD routes on one path

router
  .route("/:pasteId")
  .get(controller.read)
  .put(controller.update)
  .delete(controller.remove);

router.route("/").get(controller.list).post(controller.create);

Separate verb methods

router.get("/", controller.list);
router.post("/", controller.create);

Course example

const pastesRouter = require("../pastes/pastes.router");
router.use("/:userId/pastes", pastesRouter);

Additional references & examples

← All Building routes · Server Architecture / Routes