If you’re just getting started with building API’s, Express.js is one of the most popular frameworks in Node.js. This guide will walk you through creating a simple API with Express.js and include all the operations – GET, PUT, POST and DELETE – with examples.
By the end, you’ll have a small but working API you can run locally and expand as you learn more.
What is Express.js?
Express.js is a minimal and flexible web framework for Node.js. It makes handling HTTP requests and responses simple, allowing you to quickly set up routes, handle middleware, and build RESTful APIs.
Building a Full CRUD API with Express.js
In this tutorial, we’ll build a complete CRUD API with Express.js. CRUD stands for:
- Create → Add a new item
- Read → Fetch existing items
- Update → Modify an existing item
- Delete → Remove an item
Let’s get started!
Step 1: Express.js Project Setup
Run the following commands:
mkdir express-crud-api
cd express-crud-api
npm init -y
npm install express
Your folder structure should look like this:
express-crud-api/
│
├── node_modules/
├── package.json
├── package-lock.json
└── server.js # main server file
Step 2: Writing the CRUD API
Open server.js
and add the following code. You can copy paste the below code in the server.js file. Hopefully the inline comments should explain what the code is trying to do:
// Import express
const express = require('express');
const app = express();
const PORT = 3000;
// Middleware to parse JSON request bodies
app.use(express.json());
// In-memory "database" (just an array for demo purposes)
let items = [];
/* -------------------- READ -------------------- */
// GET /items → fetch all items
app.get('/items', (req, res) => {
res.json({
success: true,
data: items
});
});
// GET /items/:id → fetch a single item by id
app.get('/items/:id', (req, res) => {
const itemId = parseInt(req.params.id);
const item = items.find(i => i.id === itemId);
if (!item) {
return res.status(404).json({ success: false, message: 'Item not found' });
}
res.json({ success: true, data: item });
});
/* -------------------- CREATE -------------------- */
// POST /items → add a new item
app.post('/items', (req, res) => {
const { name } = req.body;
// Validate input
if (!name) {
return res.status(400).json({ success: false, message: 'Name is required' });
}
const newItem = { id: items.length + 1, name };
items.push(newItem);
res.status(201).json({
success: true,
message: 'Item created successfully',
data: newItem
});
});
/* -------------------- UPDATE -------------------- */
// PUT /items/:id → update an existing item
app.put('/items/:id', (req, res) => {
const itemId = parseInt(req.params.id);
const { name } = req.body;
const item = items.find(i => i.id === itemId);
if (!item) {
return res.status(404).json({ success: false, message: 'Item not found' });
}
if (!name) {
return res.status(400).json({ success: false, message: 'Name is required' });
}
// Update the item
item.name = name;
res.json({
success: true,
message: 'Item updated successfully',
data: item
});
});
/* -------------------- DELETE -------------------- */
// DELETE /items/:id → remove an item
app.delete('/items/:id', (req, res) => {
const itemId = parseInt(req.params.id);
const index = items.findIndex(i => i.id === itemId);
if (index === -1) {
return res.status(404).json({ success: false, message: 'Item not found' });
}
// Remove item from array
const deletedItem = items.splice(index, 1);
res.json({
success: true,
message: 'Item deleted successfully',
data: deletedItem[0]
});
});
/* -------------------- START SERVER -------------------- */
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
PS: It is highly recommended that you type the code yourself instead of doing a copy paste. This will enable you to practice the code.
Step 3: Run the Server
Once you’ve saved the server.js
file, start the server with:
node server.js
If everything works, you’ll see this in your terminal:
Server running on http://localhost:3000
Now your API is live locally and ready for testing!
Step 4: Testing the API
So, now that we’ve created the endpoints, you are ready for some testing. Hopefully, the code should be clean with no errors. You can test the endpoints using Postman, cURL, or Insomnia.
1. Create an item (POST)
POST http://localhost:3000/items
Content-Type: application/json
{
"name": "Apples"
}
2. Read all items (GET)
GET http://localhost:3000/items
3. Read one item (GET)
GET http://localhost:3000/items/1
4. Update an item (PUT)
PUT http://localhost:3000/items/1
Content-Type: application/json
{
"name": "Bananas"
}
5. Delete an item (DELETE)
DELETE http://localhost:3000/items/1
Conclusion
And that’s it — you now have a full CRUD API built with Express.js.
This version handles creating, reading (all + one), updating, and deleting items.
From here, you can:
- Replace the in-memory array with a real database like MongoDB or PostgreSQL.
- Add authentication with JWT.
- Use libraries like
express-validator
for cleaner validation.
Further Reading: How to Create a Simple API in Python (Step-by-Step Guide for Beginners)