Introduction: REST API is Simple
REST API (Representational State Transfer) is the "language" that different programs use to communicate on the internet. If you want your web application to provide data to another application (for example, a mobile client or a React frontend), you need an API. And today we will write one in JavaScript (Node.js) from scratch in just 30 minutes.
We will use the most popular library — Express. It is simple, flexible, and perfect for beginners. By the end of the article, you will have a working server with four basic CRUD operations (create, read, update, delete). Let's go!
Step 1: Setting Up the Environment and Installing Express
Before writing code, make sure you have Node.js installed. You can check this in the terminal:
node -vnpm -vIf versions are displayed — everything is ready. If not, download Node.js from the official website.
Now create a folder for the project and initialize it:
mkdir my-first-apicd my-first-apinpm init -yThe package.json file is created. Install Express and the library for working with UUID (for generating unique IDs):
npm install express uuidDone. Create a file index.js — this will be the main file of our server.
Step 2: Creating a Simple Server with Express
Open index.js and write the first version of the server:
const express = require('express');const app = express();const PORT = 3000;
// Parse JSON from request bodyapp.use(express.json());
// Test routeapp.get('/', (req, res) => { res.send('Hello, world! REST API is working!');});
// Start the serverapp.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`);});Start the server with the command:
node index.jsOpen your browser and go to http://localhost:3000. If you see the welcome message — everything is working!
Let's break down the code: express.json() is a middleware that automatically converts JSON from the request into a JavaScript object. This is needed for POST and PUT requests.
Step 3: Adding Data and CRUD Routes
Now for the interesting part — let's create an array with data (in a real project you would use a database, but for learning purposes an array will do). This will be a list of books.
const { v4: uuidv4 } = require('uuid');
// Simulated databaselet books = [];
// Create a book (POST)app.post('/api/books', (req, res) => { const { title, author } = req.body; if (!title || !author) { return res.status(400).json({ error: 'Title and author are required' }); } const newBook = { id: uuidv4(), title, author }; books.push(newBook); res.status(201).json(newBook);});
// Get all books (GET)app.get('/api/books', (req, res) => { res.json(books);});
// Get a single book by ID (GET)app.get('/api/books/:id', (req, res) => { const book = books.find(b => b.id === req.params.id); if (!book) { return res.status(404).json({ error: 'Book not found' }); } res.json(book);});
// Update a book (PUT)app.put('/api/books/:id', (req, res) => { const book = books.find(b => b.id === req.params.id); if (!book) { return res.status(404).json({ error: 'Book not found' }); } const { title, author } = req.body; if (title) book.title = title; if (author) book.author = author; res.json(book);});
// Delete a book (DELETE)app.delete('/api/books/:id', (req, res) => { const index = books.findIndex(b => b.id === req.params.id); if (index === -1) { return res.status(404).json({ error: 'Book not found' }); } books.splice(index, 1); res.status(204