Flymingo Tech - Blogs
  • Tech News
  • How-To Guides
  • Product Reviews
  • Industry Analysis
  • Cybersecurity
  • Programming
  • Development
  • Tech Lifestyle
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
Wednesday, Aug 27, 2025
Flymingo Tech - BlogsFlymingo Tech - Blogs
Font ResizerAa
  • About us
  • Privacy Policy
  • Terms & Conditions
  • Contact
Search
  • Tech News
  • How To Guide
  • Product Reviews
  • Industry Analysis
  • Cybersecurity
  • Programming
  • Development
  • Tech Lifestyle
Follow US
How-To GuidesProgramming

How to Build a Simple REST API with Node.js and Express (2025 Guide)

Hasan Hashmi
Last updated: July 11, 2025 1:17 pm
Hasan Hashmi
Share
SHARE

Building a REST API is a key skill for developers in 2025. It doesn’t matter if you’re working on a web app, backend for mobile, or microservices – a well-designed API speeds up development and helps your project grow. This guide will show you how to create a basic REST API with Node.js and Express.js even if you’re just starting out.

๐Ÿงฐ Things You Need 

  • Node.js on your computer (Get it here) 
  • A grasp of JavaScript basics
  • VS Code or another code editor 
  • Postman or a similar tool to test APIs

๐Ÿ“ Step 1: Initialize Your Project

Open your terminal and run:

mkdir simple-rest-api
cd simple-rest-api
npm init -y

This creates a basic package.json file.

๐Ÿ“ฆ Step 2: Install Express

  • Install Express.js:

    npm install express

๐Ÿ“„ Step 3: Create Your API Server

  • Create a file called index.js and paste this code:
const express = require('express');
const app = express();
const PORT = 3000;

app.use(express.json());

let users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];

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

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

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

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

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

๐Ÿงช Step 4: Test Your API

Use Postman or Hoppscotch to test these endpoints:

  • DELETE /users/:id โ€“ Delete a user
  • GET /users โ€“ List all users
  • GET /users/:id โ€“ Get user by ID
  • POST /users โ€“ Add new user (JSON body: { "name": "John" })

๐Ÿ” Step 5: Add CORS (Optional)

To allow cross-origin requests, install and enable CORS:

npm install cors

Then add to your code:

const cors = require('cors');
app.use(cors());

Congratulations! You have just developed your first working REST API using Node.js and Express. It is a basic version, but it is a scalable structure โ€“ you can add middleware, authentication, and a database! Please check back for our next guide on connecting to MongoDB.More Posts on Flymingotech.in

TAGGED:Backend DevelopmentLearn To CodeWeb Development
Share This Article
Facebook Copy Link Print
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Let's Connect

304.9kLike
3.04MFollow
8.4kFollow
LinkedInFollow

Popular Posts

How to Fix Git Merge Conflicts Like a Pro (Step-by-Step Guide)

Hasan Hashmi
5 Min Read

Git Commands You Need to Master in 2025

Hasan Hashmi
3 Min Read

Top 5 Tech Startups in India to Watch This Year

Hasan Hashmi
3 Min Read

The Ultimate Headphones for Developers Who Need Quiet (2025)

Hasan Hashmi
3 Min Read

You Might Also Like

DevelopmentHow-To GuidesTech LifestyleTech News

How to Build a Developer Portfolio That Gets Hired

4 Min Read
DevelopmentTech News

7 Best AI Coding Tools Developers Love in 2025

4 Min Read
How-To Guides

Most Popular Backend Frameworks in 2025

3 Min Read
How-To GuidesProgrammingTech News

How to Contribute to Open Source as a Beginner

4 Min Read
Flymingo Tech - Blogs
Flymingo Tech specializes in delivering innovative IT solutions, empowering businesses to enhance their operational efficiency, streamline workflows, and drive growth through the use of cutting-edge technology, customized software development, and forward-thinking digital strategies.
  • +91 7378658675
  • contact@flymingotech.com

Social Networks

Facebook-f Twitter Instagram Linkedin

ยฉ 2024 Flymingo Tech. All rights reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?