DEV Community

Cover image for A Beginner’s Guide to Building a REST API with Node.js and Express
Marien
Marien

Posted on

A Beginner’s Guide to Building a REST API with Node.js and Express

APIs power most of the apps we use every day — from social media to weather apps. A REST API (Representational State Transfer) is one of the most common ways to let applications communicate with each other. In this guide, we’ll walk step-by-step through building a simple REST API using Node.js and Express.

  • What You’ll Learn
  • What a REST API is
  • How to set up Node.js and Express
  • How to create routes for CRUD operations (Create, Read, Update, Delete)
  • How to test your API

Step 1: Initialize Your Project

First, create a new project folder and initialize npm.

mkdir rest-api-demo cd rest-api-demo npm init -y 
Enter fullscreen mode Exit fullscreen mode

Then install Express:

npm install express 
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Server

Create a file called server.js and set up a basic Express server:

const express = require('express'); const app = express(); const PORT = 3000; app.use(express.json()); // Middleware to parse JSON app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); }); 
Enter fullscreen mode Exit fullscreen mode

Run your server:

node server.js

You should see:
*Server running on http://localhost:3000
*

Step 3: Create Routes

For this example, let’s build a simple API for managing a list of books.

let books = [ { id: 1, title: "Atomic Habits", author: "James Clear" }, { id: 2, title: "The Pragmatic Programmer", author: "Andrew Hunt" } ]; // GET all books app.get('/books', (req, res) => { res.json(books); }); // GET a book by ID app.get('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); book ? res.json(book) : res.status(404).json({ message: "Book not found" }); }); // POST (Add a new book) app.post('/books', (req, res) => { const newBook = { id: books.length + 1, title: req.body.title, author: req.body.author }; books.push(newBook); res.status(201).json(newBook); }); // PUT (Update a book) app.put('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).json({ message: "Book not found" }); book.title = req.body.title; book.author = req.body.author; res.json(book); }); // DELETE a book app.delete('/books/:id', (req, res) => { books = books.filter(b => b.id !== parseInt(req.params.id)); res.json({ message: "Book deleted" }); }); 
Enter fullscreen mode Exit fullscreen mode

Step 4: Test the API

You can use tools like Postman or cURL to test your endpoints:

GET /books → Get all books GET /books/1 → Get book with ID 1 POST /books with JSON body → Add a new book PUT /books/1 with JSON body → Update book with ID 1 DELETE /books/1 → Delete book with ID 1 
Enter fullscreen mode Exit fullscreen mode

Step 5: Wrap Up

You’ve just built your first REST API with Node.js and Express. From here, you can expand it by connecting to a database (like MongoDB or PostgreSQL), adding authentication, or deploying it online.

Top comments (0)