Loading...

Warning: Undefined array key "post_id" in /home/u795416191/domains/speqto.com/public_html/wp-content/themes/specto-fresh/single.php on line 22

Building Your First REST API with Node.js and Express

Jeevan Singh

28 August, 2025

Node.js Logo


REST APIs are the backbone of modern web and mobile applications. They allow clients and servers to communicate effectively, exchanging data in a structured way. In this blog, we’ll walk through the process of building your first REST API using Node.js and Express – one of the most popular frameworks for server-side JavaScript.

Why Use Express for APIs?

While Node.js provides the core runtime, Express.js simplifies the process of building APIs by offering an easy-to-use framework. It provides routing, middleware support, and a clean structure to build scalable applications. With Express, you can create REST APIs in just a few lines of code.

Step-by-Step Guide to Building Your First REST API

1. Initialize a New Node.js Project

Create a project folder and initialize it with npm:

mkdir my-first-api
cd my-first-api
npm init -y

This sets up your project and creates a package.json file.

2. Install Express

Next, install Express.js:

npm install express

This adds Express as a dependency to your project.

3. Create Your API Server

Inside your project, create a file named server.js and add the following code:

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

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

// Sample data
let users = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Smith' }
];

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

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

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

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

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

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

This simple API allows you to perform CRUD operations (Create, Read, Update, Delete) on a list of users.

4. Test Your API

Run your server with:

node server.js

Use tools like Postman or curl to test your API endpoints:

  • GET /api/users → Fetch all users
  • GET /api/users/:id → Fetch a single user
  • POST /api/users → Add a new user
  • PUT /api/users/:id → Update a user
  • DELETE /api/users/:id → Delete a user

How This Helps You

By creating this REST API, you’ve built a foundation for more advanced applications. You can now connect your frontend (React, Angular, Vue, or even mobile apps) to this backend and manage data seamlessly. Understanding the basics of Express and REST APIs opens doors to building scalable full-stack applications.

Conclusion

Building your first REST API with Node.js and Express is a major step toward becoming a full-stack developer. With just a few lines of code, you created a functional backend that supports CRUD operations. From here, you can expand by connecting to databases, adding authentication, or deploying your API to production. Keep experimenting, and you’ll soon be building powerful backend systems!

RECENT POSTS

Beyond the Battlefield: Architecting Your Web App with Optimal SSR or CSR Rendering

Beyond the Battlefield: Architecting Your Web App with Optimal SSR or CSR Rendering Gaurav Garg 06 March 2026 In the dynamic landscape of web development, a fundamental architectural decision often dictates the success and user experience of a web application: the choice between Server-Side Rendering (SSR) and Client-Side Rendering (CSR). This isn’t merely a technical […]

How IT Companies Can Win Global Clients in 2026

How IT Companies Can Win Global Clients in 2026   Chirag Verma 06/03/2026 In 2026, the global technology market is more competitive and opportunity-rich than ever before. Businesses across industries are searching for reliable IT partners who can help them innovate, scale, and stay ahead in an increasingly digital world. For IT companies, winning global […]

The Human Side of AI: How HR Leaders Will Shape the Future of Work in 2026

The Human Side of AI: How HR Leaders Will Shape the Future of Work in 2026 Khushi Kaushik 06 march, 2026 Introduction As we step into 2026, the workplace is evolving faster than ever before. Artificial Intelligence, automation, remote work, and digital collaboration tools are transforming how organizations operate. But amid all this innovation, one […]

Socket.IO Security Unveiled: Mastering Authentication & Authorization for Robust Real-time Applications

Socket.IO Security Unveiled: Mastering Authentication & Authorization for Robust Real-time Applications Divya Pal 4 February, 2026 In the dynamic landscape of modern web development, real-time applications have become indispensable, powering everything from chat platforms to collaborative editing tools. At the heart of many of these interactive experiences lies Socket.IO, a powerful library enabling low-latency, bidirectional […]

Prisma ORM in Production: Architecting for Elite Performance and Seamless Scalability

Prisma ORM in Production: Architecting for Elite Performance and Seamless Scalability Shubham Anand 16 February 2026 In the rapidly evolving landscape of web development, database interaction stands as a critical pillar. For many modern applications, Prisma ORM has emerged as a powerful, type-safe, and intuitive tool for interacting with databases. However, transitioning from development to […]

POPULAR TAG

POPULAR CATEGORIES