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
- Introduction
- Prerequisites
- Project Setup
- Creating a Basic Server
- Building the REST API (CRUD Users)
- Testing with Postman
- Optional Improvements
- 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
π You can create this manually by:
Right-clicking in your file explorer and choosing New File (e.g., 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}`);
});
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
Creates a new user
Method: POST
Endpoint: http://localhost:5000/users
Body (raw JSON):
{
"name": "Charlie",
"email": "charlie@example.com"
}
π₯ GET /users
Returns all users.
Method: GET
Endpoint: http://localhost:5000/users
π₯ GET /users/1
Returns user with ID = 1.
Method: GET
Endpoint: http://localhost:5000/users/1
π PUT /users/2
Updates a user with ID = 2
Method: PUT
Endpoint: http://localhost:5000/users/2
Body:
{
"name": "Bobby",
"email": "bobby@newmail.com"
}
β DELETE /users/1
Deletes user with ID = 1.
Method: DELETE
Endpoint: http://localhost:5000/users/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 https://github.com/gurkeerats2/node-api-tutorial
Top comments (1)
Definately worth reading!