Whether you're a frontend developer wanting to dip your toes into backend, or a beginner who wants to understand how APIs work — this guide is for you. We'll build a simple REST API using Node.js + Express, then test it with Postman.


📚 Table of Contents

  1. Introduction
  2. Prerequisites
  3. Project Setup
  4. Creating a Basic Server
  5. Building the REST API (CRUD Users)
  6. Testing with Postman
  7. Optional Improvements
  8. Final Thoughts

🔰 Introduction

In this tutorial, you'll learn how to:

  • Create a REST API with Node.js and Express
  • Add basic CRUD operations
  • Test everything using Postman

You won’t need any database here — we’ll simulate one using an in-memory array. Perfect for learning the basics.


🧰 Prerequisites

Make sure you have the following installed:


⚙️ Project Setup

Step 1: Initialize the project

mkdir node-api-tutorial
cd node-api-tutorial
npm init -y

Step 2: Install dependencies

npm install express cors
npm install --save-dev nodemon

Step 3: Create the entry file

touch server.js

🛠️ Creating a Basic Server

Paste this code in server.js:

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

const app = express();
const port = 5000;

app.use(cors());
app.use(express.json());

app.get('/', (req, res) => {
  res.send('API is working!');
});

app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`);
});

Update your package.json to use nodemon:

"scripts": {
  "start": "nodemon server.js"
}

Then run your server:

npm start

Visit http://localhost:5000 in your browser. You should see:

API is working!


🔄 Building the REST API (CRUD Users)

Add this below the root endpoint in server.js:

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

// 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).send('User not found');
  res.json(user);
});

// POST a new user
app.post('/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 user
app.put('/users/:id', (req, res) => {
  const user = users.find(u => u.id === parseInt(req.params.id));
  if (!user) return res.status(404).send('User not found');

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

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

🧪 Testing with Postman

Open Postman and try out the following requests:

➕ POST /users

Method: POST
Body (raw JSON):

{
  "name": "Charlie",
  "email": "charlie@example.com"
}

📥 GET /users

Returns all users.


📥 GET /users/1

Returns user with ID = 1.


🛠 PUT /users/2

Method: PUT
Body:

{
  "name": "Bobby",
  "email": "bobby@newmail.com"
}

❌ DELETE /users/1

Deletes user with ID = 1.


🧩 Optional Improvements

Once you're comfortable, try adding:

✅ Input validation using Joi

💾 MongoDB + Mongoose for real persistence

🛡 Environment variables with dotenv

📘 API documentation with Swagger

🔐 JWT-based authentication


✅ Final Thoughts

You’ve just built your first REST API with Node.js and Express, and tested it with Postman. This is a great step forward for any developer — especially if you're aiming to become full-stack or start freelancing.


🐱‍💻 Source Code

👉 GitHub Repo (Replace with your actual repo)