DEV Community

Cover image for How to Build a RESTful API in Node.js and Test it with Postman
Gurkirat Singh
Gurkirat Singh

Posted on β€’ Edited on

3 2

How to Build a RESTful API in Node.js and Test it with Postman

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Install dependencies

npm install express cors
npm install --save-dev nodemon
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the entry file

πŸ“ You can create this manually by:

Right-clicking in your file explorer and choosing New File (e.g., in VS Code).

Create a new file in VS Code


πŸ› οΈ 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}`);
});
Enter fullscreen mode Exit fullscreen mode

Update your package.json to use nodemon:

"scripts": {
  "start": "nodemon server.js"
}
Enter fullscreen mode Exit fullscreen mode

Then run your server:

npm start
Enter fullscreen mode Exit fullscreen mode

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();
});
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Testing with Postman

Open Postman and try out the following requests:

βž• POST /users

Creates a new user
Method: POST
Endpoint: http://localhost:5000/users
Body (raw JSON):

{
  "name": "Charlie",
  "email": "charlie@example.com"
}
Enter fullscreen mode Exit fullscreen mode

Postman screenshot for POST endpoint


πŸ“₯ GET /users

Returns all users.
Method: GET
Endpoint: http://localhost:5000/users

Postman screenshot for GET users endpoint


πŸ“₯ GET /users/1

Returns user with ID = 1.
Method: GET
Endpoint: http://localhost:5000/users/1

Postman screenshot for GET user by ID endpoint


πŸ›  PUT /users/2

Updates a user with ID = 2
Method: PUT
Endpoint: http://localhost:5000/users/2
Body:

{
  "name": "Bobby",
  "email": "bobby@newmail.com"
}
Enter fullscreen mode Exit fullscreen mode

Postman screenshot for PUT endpoint


❌ DELETE /users/1

Deletes user with ID = 1.
Method: DELETE
Endpoint: http://localhost:5000/users/1

Postman screenshot for Delete endpoint


🧩 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 https://github.com/gurkeerats2/node-api-tutorial

Neon image

Set up a Neon project in seconds and connect from a Next.js application ⚑

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started β†’

Top comments (1)

Collapse
 
shweta_pujar_68411c098163 profile image
Shweta Pujar β€’ β€’ Edited

Definately worth reading!

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❀️