Express - basics

How to build a rest api with express

Workshop: Creating an API with Express for managing tracks and albums ๐ŸŽต

โฌ… Version franรงaise

Objective

Learn how to create a simple REST API with Express.js by gradually implementing the CRUD features (Create, Read, Update, Delete). This project will be done in a single file (index.js) to simplify the handling. The objective is also to encourage autonomy by consulting the official documentation.

Prerequisites

Have Node.js installed on your machine. If not, download it from Node.js Official. Install Express via npm.

Step 1: Initialize the project

Create a folder for the project and initialize it:

npm init -y

Install Express:

npm install express

Define the module type in your package.json:

"type": "module"

Create an index.js file that will be used for all development.

Step 2: Set up an Express server

In the index.js file, configure a minimal Express server.

Check out the documentation to start a server: ๐Ÿ‘‰ express hello world

Create a default route (GET /) that returns a simple response to verify that the server is working. Example: โ€œWelcome to my Express APIโ€.

Start the server with the following command and test your route in a browser or with a tool like Postman:

node --watch index.js

Step 3: Adding the tracks routes

Goal: Implement the following routes:

Rules :

Steps:

Add a static list of tracks (in-memory array) to your index.js file.

Example:

const tracks = [
{ id: 1, title: 'Track 1', albumId: 1 },
{ id: 2, title: 'Track 2', albumId: 1 }
];

Implement each route in the file.

See the documentation on routes:

๐Ÿ‘‰ Express Routing

Tips:

Highlights:

Enable express.json() middleware to read JSON data from request bodies. ๐Ÿ‘‰ Express JSON Middleware

Step 4: Adding Album Routes

Goal: Implement CRUD functionality for albums:

Steps:

Create a static list of albums similar to the one for tracks. Add routes for each action in index.js. Implement a specific route to retrieve the tracks of an album (GET /albums/:id/tracks).

Related documentation:

๐Ÿ‘‰ Express Params

Step 5: Data improvements and persistence

Goal: Add advanced features like:

A basic validation of the data sent (for example, check that the title of a track is not empty before adding it). Handle errors if a track or album is not found.

Helpful documentation:

๐Ÿ‘‰ Error Handling

Step 6: API Testing

Test all routes with a tool like Postman, Insomnia, or using fetch/Axios in a simple front-end application. Check that the data returned is correct and that all CRUD features work.

Step 7: Going Further

Code Structuring: Once all features are working, think about structuring the project (separate routes, controllers, models). Additional documentation: ๐Ÿ‘‰ Express architecture best practice

Conclusion

Congratulations ๐ŸŽ‰! You have built a working REST API from scratch. You have learned how to configure Express, implement routes, and manipulate data on the server side. Moving on, you can dive deeper by exploring databases like MongoDB or PostgreSQL for robust data persistence.