REST API with Express.js
MySQL Example

This Example will show you how to make a REST API using Express.js and MySQL.

The database of the example contains two tables: book and user. The user-table will be used to authorize the REST API users. So, in the exercise you will build a login system, so that the credentials will be checked based on the user.

The example follows the MVC (Model–View–Controller) pattern.

The application skeleton is created using express-generator (https://expressjs.com/en/starter/generator.html).

In a typical MVC Express application, you would have three separate folders:

  • models/ → handles database queries and data logic
  • controllers/ → contains the business logic that runs when a route is accessed
  • routes/ → maps URL paths to controller functions

In the application built in these examples, we use a simplified two-folder structure with only routes/ and models/. The route files take on the role of controllers as well — they both define the URL paths and contain the request-handling logic directly. This approach keeps the project easier to follow when learning, without splitting logic across multiple files.

To see how the book controller example would look when separated into routes and controllers folders, click the button below:



Toggle Menu