How to Build a REST API in 30 Minutes with Node.js and Express

ntroduction: Why Learn REST API Development

In today’s tech-driven world, APIs are everywhere. They connect apps, power mobile experiences, and let businesses integrate services faster than ever. If you’re a developer, building REST APIs is one of the most valuable skills you can learn — whether you’re working on a personal project, freelancing, or building enterprise-level systems.

And the good news? You don’t need weeks to get started. With Node.js (JavaScript runtime) and Express.js (a fast, minimalist web framework), you can create a REST API in less than 30 minutes.

In this tutorial, we’ll walk step by step through:
✅ Setting up your development environment
✅ Creating a simple Express server
✅ Defining routes (GET, POST, PUT, DELETE)
✅ Using Postman or cURL to test your API
✅ Adding middleware for JSON handling
✅ Best practices for structuring a production-ready API


1. What is a REST API? (Quick Overview)

REST (Representational State Transfer) is an architectural style for designing networked applications.

A REST API:

  • Uses HTTP methods like GET, POST, PUT, DELETE.
  • Returns data in JSON format (usually).
  • Is stateless (each request is independent).
  • Makes resources accessible through endpoints (URLs).

Example:

GET /api/users   → returns all users  
POST /api/users  → creates a new user  
PUT /api/users/1 → updates user with ID 1  
DELETE /api/users/1 → deletes user with ID 1  

2. Setting Up Your Development Environment

Prerequisites

  • Basic knowledge of JavaScript
  • Node.js installed on your computer (Download here)
  • A code editor (VS Code recommended)

Step 1: Create a Project Folder

mkdir rest-api-tutorial
cd rest-api-tutorial

Step 2: Initialize a Node.js Project

npm init -y

This creates a package.json file.

Step 3: Install Express

npm install express

Optional but recommended: Install nodemon for automatic server restarts:

npm install --save-dev nodemon

3. Create Your First Express Server

Create a file named server.js and add the following code:

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

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

// Root route
app.get('/', (req, res) => {
  res.send('Welcome to My REST API!');
});

// Start server
const PORT = 5000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});

Run the server:

node server.js

Visit http://localhost:5000 in your browser — you should see:
Welcome to My REST API!


4. Creating API Routes (CRUD)

Let’s build a simple API to manage a list of users.

Sample Data

let users = [
  { id: 1, name: "Alice", email: "alice@example.com" },
  { id: 2, name: "Bob", email: "bob@example.com" }
];

GET: Fetch All Users

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

GET: Fetch a Single User

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

POST: Create a New User

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

PUT: Update a User

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

  user.name = req.body.name || user.name;
  user.email = req.body.email || user.email;
  res.json(user);
});

DELETE: Remove a User

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

5. Testing Your API

Use Postman (or cURL) to test each endpoint:

  • GET http://localhost:5000/api/users → should return all users
  • POST http://localhost:5000/api/users with JSON body
{
  "name": "Charlie",
  "email": "charlie@example.com"
}

should create a new user.


6. Best Practices for REST API Development

  1. Use Proper HTTP Status Codes
    • 200 OK, 201 Created, 404 Not Found, 500 Internal Server Error.
  2. Validation
    Use libraries like Joi or Yup to validate user input.
  3. Error Handling
    Centralize error responses using middleware.
  4. Modularize Your Code
    Move routes to separate files for cleaner structure.
  5. Use Environment Variables
    Store secrets like API keys using .env files.

7. Taking It to the Next Level

Once you’ve mastered the basics:

  • Connect to a database (MongoDB, PostgreSQL).
  • Add authentication (JWT).
  • Deploy your API to cloud providers like Render, Vercel, or AWS.
  • Add rate limiting & security headers with packages like helmet.

Conclusion: Your First API in 30 Minutes

Congratulations 🎉 — you just built a REST API with Node.js and Express!

In under 30 minutes, you’ve learned:
✅ How to set up a Node.js project
✅ How to build CRUD routes
✅ How to handle JSON data
✅ How to test with Postman
✅ Best practices for building scalable APIs

This skill opens the door to backend development, SaaS projects, and freelancing opportunities.


James

Recent Posts

7 Captivating Insights from B2B SaaS Reviews’ Founder on Online Reviews

The Importance of Customer Reviews in Software Purchases It's no secret that customer reviews play…

12 hours ago

How to Quickly Copy and Replicate n8n Workflows Using Claude AI

![AI-powered tool simplifying n8n workflow automation](https://www.geeky-gadgets.com/wp-content/uploads/2025/04/ai-powered-n8n-automation-guide.webp) Have you ever wished you could replicate a complex…

12 hours ago

Strategies for Creating Future-Ready Cybersecurity Teams

The Democratization of Cybersecurity: Navigating AI-Enhanced Cyber Threats We are witnessing something unprecedented in cybersecurity:…

12 hours ago

The Leading 5 CPG Technology Trends Transforming 2026

The Top 5 CPG Tech Trends Shaping 2026 By Lesley Salmon, Global Chief Digital &…

12 hours ago

Must-Grab Tech Deals After Cyber Monday

Must-Have Tech Gadgets for Your Life In the fast-paced world we live in, staying connected…

13 hours ago

AWS Enters the Security AI Agent Competition Alongside Microsoft and Google • The Register

AWS Security Agent: Ushering in a New Era of Application Security As part of its…

13 hours ago