API Design in Node.js, v5

Scott Moss
Netflix
9 hours, 58 minutes CC
API Design in Node.js, v5

Course Description

Build scalable APIs for any application with Node.js and Express. Learn RESTful API design best practices as you design routes, run Postgres migrations, and secure your API with JWT-based authentication. Bullet-proof your API with TypeScript, runtime schema validation with Zod, and comprehensive integration testing strategies. Create a production deployment and ship your next API today!

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: September 29, 2025

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 250+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops

Table of Contents

Introduction

Section Duration: 24 minutes
  • Introduction
    Scott Moss introduces the course by sharing a bit about his background and teaching style. He outlines the course goals, including building a habit-tracking API, using Node.js, Express, TypeScript, and Postgres, and covering topics like RESTful design, authentication, testing, and deployment.
  • API Project Tour
    Scott outlines the API project, which will cover controllers, databases, middleware, routes, Postman testing, integration tests, seeding data, and JSON APIs.
  • Node.js & Servers Overview
    Scott explains the differences between Node.js and browser JavaScript, noting the browser’s focus on DOM and HTTP calls versus Node.js’s server-side tools like file access and server building. He also covers package.json for dependencies and tsconfig.json for TypeScript setup.

Getting Started with Express

Section Duration: 57 minutes

Creating Express Routes

Section Duration: 1 hour, 1 minute
  • HTTP Verbs & RESTful Route Patterns
    Scott explains common HTTP methods like GET, POST, PUT, PATCH, and DELETE, their purposes in APIs, and the effect of each request. He also touches on REST principles, CRUD routes, and the flexibility of REST compared to GraphQL.
  • Requests and Responses
    Scott explains that requests to unhandled routes cause 404 errors, while not responding can hang the server. He emphasizes understanding HTTP as a client-server system and the constraints affecting real-time communication and dynamic Express paths.
  • Subrouters
    Scott demonstrates how to create sub-routers in Express for resources like users, habits, and authentication, setting up CRUD routes and handling habit completions. He also covers HTTP status codes, emphasizing proper use of success and error responses.
  • User Routes
    Scott reviews creating user routes in Express, stressing practice and rewriting code. He covers creating, updating, and deleting users, admin and authentication considerations, and additional CRUD operations for completeness.
  • Mounting Routes with use Middleware
    Scott explains mounting routers in Express using .use for routes like /api/auth, /api/users, and /api/habits, highlighting the decoupling benefits. He also notes that Express handles responses and errors without enforcing REST-specific actions for HTTP verbs.
  • Advanced Routing Strategies
    Scott demonstrates creating nested routers in Express for API segmentation, highlighting route collision prevention with enums or globals. He also covers conditional routes, feature flags, API versioning, and organizing routes by resource or feature for cleaner structure.

Middleware

Section Duration: 1 hour, 7 minutes

Postgres Databases Setup

Section Duration: 1 hour, 54 minutes
  • Database Schemas
    Scott explains database schema design, focusing on simplicity, relational concepts, and modeling data around ownership and user journeys. He highlights primary and foreign keys, relationships, and a purpose-driven approach to data modeling.
  • PostgreSQL & ORMs
    Scott discusses Postgres as a versatile open-source database with features like JSON and geospatial search. He emphasizes using Node.js with Postgres for its ecosystem and ORM support, explaining ORMs provide type safety and help prevent SQL injection.
  • Database Migrations
    Scott explains database migrations for maintaining consistency and avoiding application-breaking changes. He covers schema and data migrations, prefers non-destructive changes, and highlights using Drizzle over Prisma for TypeScript flexibility.
  • Create Tables with Drizzle ORM
    Scott demonstrates creating tables using Drizzle ORM, defining columns, primary and foreign keys, data types, constraints, indexes, and relationships. He shows examples for users, habits, entries, tags, and a many-to-many table.
  • Establish Table Relationships
    Scott walks through setting up one-to-many and many-to-many relationships between tables, specifying linking fields, and avoiding circular dependencies. He also suggests splitting schemas into separate files for larger projects.
  • Exporting Database Types & Schemas
    Scott demonstrates exporting TypeScript types and Zod schemas for tables like user, habit, entry, and tag. He shows validating inputs against database rules to ensure data consistency at runtime.
  • Schema Best Practices
    Scott discusses best practices for database schemas, including adding timestamps, enforcing constraints, and using descriptive names. He also recommends avoiding destructive changes in database migrations by following an additive approach.
  • Hosted PostgreSQL with Neon
    Scott walks through setting up a Postgres database with Neon, configuring the database URL in .env, and creating a connection with pooling. He discusses avoiding memory leaks and using service databases in development.
  • Database NPM Scripts
    Scott explains adding scripts in package.json to manage the database outside the ORM, including syncing, migrating, seeding, and using Drizzle Studio. He demonstrates syncing the schema and visualizing the database.
  • Seed the Database
    Scott discusses using seed scripts to populate databases for testing, development, or reproducing issues. He also demonstrates creating flexible seed scripts while ensuring data integrity and avoiding production runs.
  • Database Q&A
    Scott explains how Drizzle Studio works with Drizzle Kit, managing large seed data in separate files for testing. He also suggests being careful schema changes in production and generating single migration files for multiple changes.

Authentication & Authorization

Section Duration: 1 hour, 43 minutes

CRUD Routes

Section Duration: 46 minutes
  • RESTful Route Design Review
    Scott reviews RESTful route design, explaining how CRUD controllers map to resources like habits and users. He discusses how requests flow through middleware for authentication and validation before reaching controllers, and he reviews common HTTP status codes such as 200 for success, 401 for authentication errors, 404 for missing resources, and 500 for server errors.
  • Create Habit Controller
    Scott explains how to create a habit controller by working with database tables, managing relations like habit tags, and using transactions to maintain integrity. He also shows how to create a habit, handle errors, validate inputs with schemas, and connect the controller to the habit route.
  • Get All Habits Controller
    Scott creates a getUserHabits function to retrieve all habits for a user, joining tables and transforming data in JavaScript to include tags. He also notes that transformations are usually done in the database for efficiency, and that GET requests do not require input validation.
  • Update Habit Controller
    Scott explains how to update a habit by separating tag IDs from the request body, running a transaction to ensure integrity, and checking authorization before saving changes. He also covers managing tag associations, returning proper responses, and handling errors during the process.
  • Testing the CRUD Routes
    Scott demonstrates using Postman to test creating, retrieving, and updating habits. He also shows how to structure JSON requests.

Error Handling & Testing

Section Duration: 1 hour, 23 minutes

Production Deployment & CI/CD

Section Duration: 24 minutes

Wrapping Up

Section Duration: 14 minutes
  • Wrapping Up
    Scott wraps up the course by suggesting further learning with tools like GraphQL, gRPC, serverless APIs, Web Sockets, Docker, and GitHub Actions. He recommends students continue experimenting, expanding on the API, and using it as a reference.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now