Introduction: REST API in JavaScript — Fast and Simple
Modern web development is unthinkable without APIs. REST (Representational State Transfer) is the most popular architectural style for creating web services. If you want to quickly build a backend for your application or learn the basics of server-side development, Node.js and Express are the ideal choice.
In this article, we will write a full-fledged REST API in JavaScript in 30 minutes. You will learn how to set up a server, organize routing, implement CRUD operations (Create, Read, Update, Delete) and test the API using Postman. No unnecessary theory — just practice. Let's go!
1. Environment Setup and Dependency Installation
Before writing code, make sure Node.js (version 14 or higher) is installed on your computer. You can check this with the command:
node -vnpm -vCreate a new folder for the project and initialize package.json:
mkdir my-rest-apicd my-rest-apinpm init -yNow let's install Express — a minimal and flexible framework for Node.js. Also install body-parser for processing JSON requests (in modern versions of Express it is built-in, but for clarity we will add it explicitly):
npm install express body-parserThe project structure will be simple:
app.js— main server fileroutes/— folder with routesdata/— folder with test data (database simulation)
2. Creating a Basic Server with Express
Create a file app.js and write the following code:
const express = require('express');const bodyParser = require('body-parser');const app = express();const PORT = 3000;
// Middleware for processing JSONapp.use(bodyParser.json());
// Simple test routeapp.get('/', (req, res) => { res.send('REST API is working!');});
// Start the serverapp.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`);});Start the server with the command node app.js and open http://localhost:3000 in your browser. You should see the message "REST API is working!". Congratulations, you have just created your first web server on Node.js!
Note: body-parser converts incoming data to JSON format, which is necessary for POST and PUT requests.
3. Organizing Routing and CRUD Operations
Now let's add real data. Create a data folder and inside it a file items.js with a test array:
const items = [ { id: 1, name: 'Learn Node.js', completed: false }, { id: 2, name: 'Write API', completed: true }, { id: 3, name: 'Publish article', completed: false }];
module.exports = items;Create a routes folder and a file items.js inside it. Here we will describe all routes for working with tasks (to-do list):
const express = require('express');const router = express.Router();let items = require('../data/items');
// GET /api/items — get all itemsrouter.get('/', (req, res) => { res.json(items);});
// GET /api/items/:id — get item by IDrouter.get('/:id', (req, res) => { const id = parseInt(req.params.id); const item = items.find(i => i.id === id); if (!item) return res.status(404).json({ message: 'Item not found' }); res.json(item);});
// POST /api/items — create a new itemrouter.post('/', (req, res) => { const newItem = { id: items.length + 1, name: req.body.name, completed: req.body.completed || false }; items.push(newItem); res.status(201).json(newItem);});
// PUT /api/items/:id — update an itemrouter.put('/:i