REST API with Express.js

This is a tutorial about building a REST API using Node.js, Express.js, JavaScript, and MySQL database.

Nodejs

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.

Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It is particularly well-suited for building REST APIs, as it simplifies routing, middleware integration, and HTTP request handling.

MySQL

MySQL is a popular open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) to manage and organize data. In a REST API, MySQL serves as the persistent data storage layer where application data is stored in tables with defined relationships.

MySQL is well-suited for REST APIs because it:

  • Provides reliable data storage with ACID (Atomicity, Consistency, Isolation, Durability) properties
  • Supports complex queries and relationships between data tables
  • Scales well for applications of various sizes
  • Integrates easily with Node.js through libraries like mysql2
REST API

A RESTful API is an architectural style for an application programming interface (API) that uses HTTP requests to access and manipulate data. The main HTTP methods used in REST APIs are:

  • GET - Retrieve/read data from the server
  • POST - Create new resources
  • PUT - Replace/update an entire resource
  • PATCH - Partially update a resource
  • DELETE - Remove a resource

Example REST API Structure:

For a user management system, typical endpoints might look like:

  • GET /api/users - Get all users
  • GET /api/users/:id - Get a specific user by ID
  • POST /api/users - Create a new user
  • PUT /api/users/:id - Update an entire user record
  • PATCH /api/users/:id - Update specific fields of a user
  • DELETE /api/users/:id - Delete a user
Frontend and Backend

A REST API is a backend application. In programming, the terms frontend and backend refer to different parts of a software application, each handling specific responsibilities.

Frontend:

  • The frontend is the user interface, or client-side, of the application that users interact with.
  • It includes everything users see and interact with, such as buttons, forms, and other UI elements.
  • Common frontend technologies include HTML, CSS, and JavaScript, along with frameworks like React, Angular, and Vue.js.

Backend:

  • The backend is the server-side of the application, responsible for data management, business logic, and communication with the database.
  • It handles tasks such as database operations, authentication, and API endpoints.
  • Common backend technologies include programming languages like Python, Java, and Ruby, as well as frameworks like Node.js, Django, and Spring Boot.
  • Databases such as MySQL, PostgreSQL, and MongoDB are often used to store and manage data.

In summary, the frontend is responsible for the user experience, while the backend manages behind-the-scenes processes that enable functionality and data interaction.

Full-Stack Development:

A full-stack developer works with both the frontend and backend, bridging the gap between the user interface and server-side operations. Full-stack development involves using a combination of frontend and backend technologies to build complete applications.

Client-Side vs. Server-Side

In web development, programming languages can be classified as client-side or server-side. This classification refers to where the code is executed: on the client (the user's device) or the server.

Browser (User)

Click the buttons to see how PHP and JavaScript work differently.

JavaScript was traditionally a client-side language executed in web browsers. However, with the introduction of Node.js, it can also be executed on the server.



Toggle Menu