API Design in Node.js, v4

Scott Moss

Scott Moss

Superfilter AI
7 hours, 4 minutes CC
API Design in Node.js, v4

Course Description

Design and build APIs from the ground up in Node.js! Use Express to handle routes and create your REST API. You'll read and update from a Postgres database using Prisma and TypeScript. Then add authentication to lock down your API with JWTs. Finally, learn how to deploy your API for the world to see!

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

Preview
Close

Course Details

Published: November 3, 2022

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Introduction

Section Duration: 8 minutes

API Basics in Node.js

Section Duration: 37 minutes
  • Creating an HTTP Server
    Scott creates a basic HTTP server using the built-in http Node module. As requests are made to the server, conditional logic determines how the server should respond.
  • Requests & Responses
    Scott examines the request and response objects passed to the createServer callback by the http module. Since the request is being handled by the server, no XHR request will appear in the browser. Server-side JavaScript executes in the Node runtime on the server and not in the browser.
  • Anatomy of an API
    Scott explains the different components of an API including HTTP methods, route handlers, and how IP addresses and ports work. Most of the work of an API happens inside the route handler. These handlers can contain logic for determining how to handle requests, authentication, and limit the number of requests to protect the server.
  • Creating a Server with Express
    Scott refactors the code to use Express instead of the built-in http module. Express is installed with NPM and provides helper methods for defining routes. Depending on the functionality of the API, route handlers can return anything from basic text or JSON, to HTML files.

Prisma

Section Duration: 46 minutes
  • Object Relational Mapper (ORM)
    Scott introduces ORMs, or Object Relational Mappers. ORMs provide an API for conducting database operations and eliminate the need to write SQL statements inside the application. A question about using JSON in Postgres vs. Mongo is also answered in this lesson.
  • Prisma & Render Setup
    Scott explains how Prisma, a database-agnostic ORM, will be used in the application. A PostgreSQL database is created on Render.com. Prisma communicates between the application and Render.com using the external database URL located in the Render.com connection settings.
  • Prisma Overview
    Scott installs Prisma and runs the CLI tool to generate the required files for the project. The prisma.schema file contains all the schemas for the data models. The database connection string is added to the .env file.
  • Designing a Schema
    Scott creates a schema for the User model. Each user has an id, username, password, and a createdAt field. The id field uses a UUID, a unique string that can be generated by Prisma when a user is created.
  • Product Model
    Scott creates the schema for the Product model. A relationship between the Product and User models is established using the Prisma @relation attribute.
  • Update & UpdatePoint Models
    Scott finishes the last two models, Update and UpdatePoint. Relationships are established between the two models. A question about how Prisma supports multiple database platforms is also answered in this lesson.
  • Migrations
    Scott runs a migration that syncs the database structure with the Prisma schema. A migrations directory is also created which stores every executed migration. The benefit of having migration files is the ability to track them with source control and ensure every developer is aware of changes made to the database.

Routes & Middleware

Section Duration: 45 minutes
  • Defining Routes
    Scott explains the CRUD operations Create, Read, Update, and Delete. The routes required to execute these operations are created for the Product, Update, and UpdatePoint models. A question about GRPC vs REST is also answered in this lesson.
  • Importing the Application Router
    Scott mounts the router in the main application and configures it to be used under the "/api" parent route. This architecture makes the application more modular because the business logic for different areas of the application can be defined in separate routers.
  • Testing the API with Thunder Client
    Scott demonstrates using the VSCode extension Thunder Client to test API endpoints. An endpoint needs to return something. Otherwise, the request will hang. With HTTP, the connection with the server is closed once something is returned.
  • Middleware
    Scott introduces the concept of middleware and installs Morgan, a logging middleware for Express. Middleware is a function that can be called for any request or only specific routes. The middleware function calls a next() method when it's complete so the request can finish being processed by the server.
  • Creating a Custom Middleware
    Scott creates a custom middleware that adds a property to the request object. This property is now available to any subsequent middleware and the route handler.

Authentication

Section Duration: 1 hour, 2 minutes

Route & Error Handlers

Section Duration: 2 hours, 26 minutes

Config, Performance, & Testing

Section Duration: 53 minutes
  • Environment Variables
    Scott introduces environment variables. The NODE_ENV variable lets the application and any other package know the environment. This could be development, testing, production, or any other string. Using custom environment variables for database URIs or secrets allows confidential information to be injected into the application based on the environment.
  • Creating Environment Configurations
    Scott creates configuration files for each environment and uses the lodash.merge module to merge custom environment variables into a single configuration object. This architecture allows a default configuration to be created, and for each environment, a customization for a specific environment can override the default.
  • Performance Management with Async
    Scott explains the difference between blocking and nonblocking code. Whenever possible, developers should use asynchronous APIs because they are nonblocking and more performant for the server when executing multiple requests.
  • Unit Testing Overview
    Scott describes unit tests as testing individual pieces of logic independently. In order for unit tests to be effective, the code should be written in a testable way. For example, function arguments are easier to test than closure variables.
  • Unit Testing with Jest
    Scott creates a __tests__ directory and writes a unit test. The describe block is a wrapper around one or more tests. The "it" method contains a statement about what outcome is expected for the individual test.
  • Integration Testing with SuperTest
    Scott demonstrates how integration tests can be used to test an entire route's functionality. The supertest module provides methods for building requests and validating the result of those requests.
  • Testing the Create User Route
    Scott uses the supertest module to write an integration test for the createNewUser route handler. A mock object is sent along with the request with the username and password. The importance of using a testing database is also discussed in this lesson.

Deployment

Section Duration: 18 minutes

Wrapping Up

Section Duration: 5 minutes
Just finished the, "API Design in Node.js, v4" course at Frontend Masters. It has been a valuable one. I learned API development with Express.js, TypeScript, Prisma, and Postgres in Node.js.
Salman Sajid

Salman Sajid

Consultant Development @ Systems Limited | Vue.js, React.js, Next.js, Typescript

I've just been offered my first role. I can now call myself a "software developer", and Frontend Masters has been a huge part of that. Particularly Scott Moss' API design course made me impress massively in my tech test, and pretty much got me the job. thanks so much to everyone at frontend masters for everything you do ❤️
Harry

Harry

Harry

Just now completed the API Design in Node.js, V4 course by Scott Moss on Frontend Masters. Wanna appreciate how good this course actually is and absolutely love the way Scott teaches and explains every single thing. Great course. Highly recommended.
sky

sky

gora_badan

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