๐ŸŒ Node.js Express & API Development
Estimated reading: 4 minutes 426 views

Node.js โ€“ RESTful API with Express โ€“ Build Scalable APIs with CRUD Operations


Introduction โ€“ What Is a RESTful API in Node.js?

A RESTful API (Representational State Transfer) allows you to interact with backend data through standardized HTTP methods like GET, POST, PUT, and DELETE. Using Express.js, Node.js makes it easy to create lightweight, maintainable, and powerful APIs that serve as the backend for web and mobile applications.

In this guide, youโ€™ll learn:

  • What REST API architecture looks like in Express
  • How to define routes for CRUD operations
  • Handle JSON input/output
  • Use best practices for building scalable APIs

Setup โ€“ Create Express App for REST API

1. Initialize the project:

npm init -y
npm install express

2. Create server.js:

const express = require('express');
const app = express();
const PORT = 3000;

// Middleware to parse JSON bodies
app.use(express.json());

// Sample in-memory data
let users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

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

// GET user by ID
app.get('/api/users/:id', (req, res) => {
  const user = users.find(u => u.id == req.params.id);
  user ? res.json(user) : res.status(404).send('User not found');
});

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

// PUT update user
app.put('/api/users/:id', (req, res) => {
  const user = users.find(u => u.id == req.params.id);
  if (user) {
    user.name = req.body.name;
    res.json(user);
  } else {
    res.status(404).send('User not found');
  }
});

// DELETE user
app.delete('/api/users/:id', (req, res) => {
  users = users.filter(u => u.id != req.params.id);
  res.send('User deleted');
});

// Start server
app.listen(PORT, () => console.log(`API running at http://localhost:${PORT}`));

Output โ€“ API Test Examples (Using Postman or Curl)

GET /api/users

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob" }
]

POST /api/users with { "name": "Charlie" }

{ "id": 3, "name": "Charlie" }

PUT /api/users/2 with { "name": "Bobby" }

{ "id": 2, "name": "Bobby" }

DELETE /api/users/1

User deleted

REST API Methods Overview

MethodRoutePurpose
GET/api/usersFetch all users
GET/api/users/:idFetch user by ID
POST/api/usersCreate new user
PUT/api/users/:idUpdate user by ID
DELETE/api/users/:idDelete user by ID

Best Practices for RESTful API in Express

Practice Benefit
Use express.json() middlewareAutomatically parses JSON requests
Validate user inputPrevents invalid or dangerous data
Use status codes (200, 201, 404)Makes API responses meaningful
Separate logic into routes/controllersKeeps code organized and testable
Version your API (/api/v1/...)Makes upgrading your API easier

Summary โ€“ Recap & Next Steps

Building a RESTful API with Express is straightforward and powerful. You learned to handle all major HTTP methods and create a mock CRUD system using in-memory data.

Key Takeaways:

  • Express makes it easy to build RESTful APIs in Node.js
  • Define GET, POST, PUT, and DELETE routes for full CRUD
  • Parse incoming data with express.json()
  • Use clear status codes and route patterns

Real-world relevance:
Used in eCommerce APIs, user authentication systems, content platforms, admin panels, mobile backends, and more.


FAQs โ€“ RESTful API with Express in Node.js


Do I need a database to build a REST API?
No. You can start with in-memory arrays for testing and switch to a database like MongoDB or MySQL later.


What does express.json() do?
It parses incoming JSON request bodies so req.body is automatically available.


Can I deploy this API to the cloud?
Yes. You can host this on platforms like Heroku, Vercel, Render, or even AWS.


How can I add validation to incoming data?
Use packages like joi, express-validator, or custom middleware to validate inputs.


What tools should I use to test REST APIs?
Use Postman, curl, or VSCode REST Client for local testing.


Share Now :
Share

๐Ÿงฌ Node.js โ€“ RESTful API with Express

Or Copy Link

CONTENTS
Scroll to Top