Introduction: Why You Need a REST API in JavaScript in 30 Minutes?
\nModern web development is unthinkable without an API (Application Programming Interface). REST (Representational State Transfer) is the most popular architectural style for creating web services. If you want to quickly master backend development or launch your first server, JavaScript and Node.js are the ideal choice. In 30 minutes, you can create a full-fledged REST API that will process requests, return data in JSON format, and work with a database (in our example, with a simple in-memory array).
\nIn this article, we will break down a practical example: we will create an API for managing a to-do list. You will learn how to set up a server, handle GET, POST, PUT, and DELETE requests, and add basic validation. By the end of reading, you will have ready-to-run code that you can immediately test.
\n\nStep 1: Project Setup and Dependency Installation
\nBefore writing code, you need to prepare the environment. Make sure you have Node.js installed (you can download it from the official website). Then follow the instructions:
\n- \n
- Create a new folder for the project, for example
rest-api-todo. \n - Open a terminal in this folder and run the command
npm init -y— this will create apackage.jsonfile. \n - Install Express, the most popular framework for Node.js:
npm install express. \n - For convenience, install
nodemonas a dev dependency:npm install --save-dev nodemon. It will automatically restart the server when code changes. \n
After installation, add a script to package.json for launching:
"scripts": {\n "start": "node index.js",\n "dev": "nodemon index.js"\n}\n\nStep 2: Creating a Basic Server with Express
\nNow let's create a file index.js — this will be the entry point for our application. In it, we will set up the server, connect middleware for working with JSON, and define the first route.
const express = require('express');\nconst app = express();\nconst PORT = 3000;\n\n// Middleware for parsing JSON\napp.use(express.json());\n\n// Temporary data storage (instead of a database)\nlet tasks = [\n { id: 1, title: 'Learn Express', completed: false },\n { id: 2, title: 'Write a REST API', completed: false }\n];\nlet currentId = 3;\n\n// Basic route for testing\napp.get('/', (req, res) => {\n res.send('REST API is working! Use /api/tasks');\n});\n\n// Start the server\napp.listen(PORT, () => {\n console.log(`Server running at http://localhost:${PORT}`);\n});\nStart the server with the command npm run dev and open http://localhost:3000 in your browser. You should see the message "REST API is working!".
Step 3: Implementing CRUD Operations (GET, POST, PUT, DELETE)
\nNow for the most interesting part — let's create a full-fledged REST API for managing tasks. We will implement four main HTTP methods:
\n\n3.1. GET /api/tasks — Retrieving all tasks
\n// Get all tasks\napp.get('/api/tasks', (req, res) => {\n res.json(tasks);\n});\n\n3.2. GET /api/tasks/:id — Retrieving a single task by ID
\n// Get a task by ID\napp.get('/api/tasks/:id', (req, res) => {\n const id = parseInt(req.params.id);\n const task = tasks.find(t => t.id === id);\n \n if (!task) {\n return res.status(404).json({ message: 'Task not found' });\n }\n res.json(task);\n});\n\n3.3. POST /api/tasks — Creating a new task
\n// Create a new task\napp.post('/api/tasks', (req, res) => {\n const { title, completed } = req.body;\n \n // Validation\n if (!title || typeof title !== 'string') {\n return res.status(400).json({ message: 'Task title is required and must be a string' });