3 Topics
Chapter•55 Minutes

Node.js Backend and API Engineering

Layered APIs, identity, tenant authorization, jobs, webhooks, retries, and idempotency

Controllers, Services, and Repositories

Module:Core concept

Controllers handle HTTP; services implement use cases; repositories encapsulate persistence; adapters integrate external providers.

How to apply it

Use a request flow of middleware, authentication, authorization, validation, controller, service, repository/transaction, and response. Inject dependencies explicitly and normalize errors centrally.

Worked example

A project controller validates request shape and calls a project service; the service enforces domain invariants before persistence.

Common mistakes to avoid

Database calls scattered across controllers or hidden business rules in middleware.

Practice task

Implement this concept in the capstone. Add one positive test, one negative or failure-path test, and a short note explaining the trade-off you chose.

Key Takeaways
  • Controllers handle HTTP; services implement use cases; repositories encapsulate persistence; adapters integrate external providers.
  • Use a request flow of middleware, authentication, authorization, validation, controller, service, repository/transaction, and response.
Knowledge Check

What is the strongest production practice for Controllers, Services, and Repositories?

Authentication and Authorization

Module:Core concept

Authentication establishes identity; authorization decides what the identity may access.

How to apply it

Use secure sessions or suitable OAuth/OIDC flows. Configure cookie attributes, expiry, rotation, revocation, and CSRF defenses. Hash passwords with a modern password-hashing algorithm. Enforce deny-by-default RBAC plus resource and tenant checks on every protected operation.

Worked example

Verify active membership, query a project by both project ID and organization ID, and check capability before returning it.

Common mistakes to avoid

Storing plaintext passwords, logging tokens, trusting client roles, or checking only that the user is logged in.

Practice task

Implement this concept in the capstone. Add one positive test, one negative or failure-path test, and a short note explaining the trade-off you chose.

Key Takeaways
  • Authentication establishes identity; authorization decides what the identity may access.
  • Use secure sessions or suitable OAuth/OIDC flows.
Knowledge Check

What is the strongest production practice for Authentication and Authorization?

Background Jobs and Webhooks

Module:Core concept

Slow work such as email, exports, and provider events should be durable asynchronous work when it need not block a request.

How to apply it

Use stable job/event IDs, tenant context, bounded retries with exponential backoff and jitter, and a failed-job workflow. Verify webhook signatures against the raw body, persist unique event IDs, acknowledge after durable acceptance, and process idempotently.

Worked example

A unique provider event ID prevents a retried payment webhook from applying the same transition twice; reconciliation repairs missed events.

Common mistakes to avoid

Assuming webhook delivery is ordered or that a timeout proves an operation did not happen.

Practice task

Implement this concept in the capstone. Add one positive test, one negative or failure-path test, and a short note explaining the trade-off you chose.

Key Takeaways
  • Slow work such as email, exports, and provider events should be durable asynchronous work when it need not block a request.
  • Use stable job/event IDs, tenant context, bounded retries with exponential backoff and jitter, and a failed-job workflow.
Knowledge Check

What is the strongest production practice for Background Jobs and Webhooks?