CRUD Operations in Express.js

📘 Express.js 👁 57 views 📅 Nov 05, 2025
⏱ Estimated reading time: 3 min

CRUD stands for Create, Read, Update, Delete, which are the basic operations for managing data. Express.js, combined with a database like MongoDB, MySQL, or PostgreSQL, makes building CRUD applications straightforward.


1. Setting Up Express

const express = require('express'); const app = express(); const port = 3000; // Middleware to parse JSON app.use(express.json()); // Dummy data let users = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ]; app.listen(port, () => console.log(`Server running at http://localhost:${port}`));

2. Create (POST)

Add a new resource.

app.post('/users', (req, res) => { const newUser = { id: users.length + 1, name: req.body.name }; users.push(newUser); res.status(201).json(newUser); });
  • URL: POST /users

  • Body: { "name": "Charlie" }

  • Response: Newly created user object


3. Read (GET)

Get all users:

app.get('/users', (req, res) => { res.json(users); });

Get user by ID:

app.get('/users/:id', (req, res) => { const user = users.find(u => u.id === parseInt(req.params.id)); if (!user) return res.status(404).json({ error: 'User not found' }); res.json(user); });

4. Update (PUT / PATCH)

Update an entire user (PUT):

app.put('/users/:id', (req, res) => { const user = users.find(u => u.id === parseInt(req.params.id)); if (!user) return res.status(404).json({ error: 'User not found' }); user.name = req.body.name; res.json(user); });

Update part of a user (PATCH):

app.patch('/users/:id', (req, res) => { const user = users.find(u => u.id === parseInt(req.params.id)); if (!user) return res.status(404).json({ error: 'User not found' }); if (req.body.name) user.name = req.body.name; res.json(user); });

5. Delete (DELETE)

Remove a resource.

app.delete('/users/:id', (req, res) => { const index = users.findIndex(u => u.id === parseInt(req.params.id)); if (index === -1) return res.status(404).json({ error: 'User not found' }); const deletedUser = users.splice(index, 1); res.json(deletedUser[0]); });

6. Testing CRUD Operations

  • Use Postman or Insomnia to test API endpoints.

  • Example workflow:

    1. POST /users → Create a user

    2. GET /users → List all users

    3. GET /users/:id → Get a user

    4. PUT /users/:id → Update user

    5. DELETE /users/:id → Remove user


7. Best Practices

  1. Use HTTP status codes correctly:

    • 200 OK, 201 Created, 404 Not Found, 400 Bad Request

  2. Validate user input before creating or updating records

  3. Use Express Router to separate CRUD routes by resource

  4. Connect CRUD operations to a real database for persistent storage

  5. Handle errors using middleware


8. Example Project Structure

express-crud/ │ ├─ routes/ │ └─ users.js │ ├─ controllers/ │ └─ userController.js │ └─ app.js
  • app.js → Main server setup

  • routes/users.js → CRUD routes

  • controllers/userController.js → Route logic


CRUD operations are the foundation of any backend application, and mastering them in Express.js is essential before moving to databases and authentication.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes