Introduction (Bookstore)
Welcome, everyone.
In this course, we’re going to learn how to use the Express framework to build a JSON API. The API will serve the most basic operations used in almost every application: Create, Read, Update, and Delete – commonly known as CRUD.
What is Express?
Express is a web framework for Node.js. Node.js allows us to run JavaScript on the server side. With Express, we can easily build server-side applications, including APIs.
Course Layout
Here is how this course is structured:
- Introduction to the Data Structure: We’ll examine the data we’ll be working with.
- Generating an Express Project: Learn how to set up a new Express project.
- Writing the First Express Project: Start coding with Express.
- Running the Server: Understand how to start the Express server.
- Creating Routes: Learn how to handle API endpoints, including:
- Fetching all resources.
- Fetching a specific resource by ID.
- Updating a resource.
- Deleting a resource.
Introduction to the Data Structure
Let’s take a closer look at the data structure we’ll be using in this course. To keep things simple and focus on Express, we’ll use hardcoded data instead of a database. This allows us to avoid dealing with database configuration and focus entirely on building the API.
Imagine you’re working for a bookstore, and you need to build an API to perform CRUD operations on a resource called “book.”
Properties of a Book
A book will have the following properties:
- ID: A unique identifier.
- Title: The title of the book.
Although books may have additional properties, we’ll keep it simple to focus on building the API.
Example Data Structure
Here is an example of the data we’ll work with:
const books = [
{ id: 1, title: "To Kill a Mockingbird" },
{ id: 2, title: "1984" },
{ id: 3, title: "The Great Gatsby" }
];
Think of this array as our database. Each object represents a record, similar to a row in a SQL table.
Operations
With our API, we will be able to:
- Fetch All Books: Retrieve the entire list of books.
- Example endpoint:
/books
- Example endpoint:
- Fetch a Specific Book: Retrieve a book by its ID. For example, requesting the book with ID 2 would return:
{ "id": 2, "title": "1984" }
- Example endpoint:
/books/:id
- Example endpoint:
- Update a Book: Modify a book’s details, such as updating the title to “Ron Quixote.”
- Example endpoint:
/books/:id
- Method:
PUT
- Example endpoint:
- Delete a Book: Remove a book record from the data structure.
- Example endpoint:
/books/:id
- Method:
DELETE
- Example endpoint:
What’s Next?
Now that we’ve covered the data structure, it’s time to start building our project. In the next lesson, we’ll generate an Express project and begin writing our code.