DEV Community

Cover image for Building a Simple API with Node.js and Express
Chukwunonso Joseph Ofodile
Chukwunonso Joseph Ofodile

Posted on

Building a Simple API with Node.js and Express

When I first started learning backend development, the idea of creating an API felt intimidating.
I thought you needed huge servers, complex databases, and lots of code to make one work.

But then I discovered Node.js and Express, and everything changed.

In this post, I’ll show you how I built my first simple API using Node.js and Express, step by step no complicated setup, just clear and practical learning.

What You’ll Need

Before we start, make sure you have:

  1. Node.js installed on your computer

  2. A code editor like VS Code

  3. Basic understanding of JavaScript

To check if Node.js is installed, open your terminal and run:

node -v

Enter fullscreen mode Exit fullscreen mode

If you see a version number, you’re good to go!

Step 1: Setting Up the Project

First, create a new folder for your project:

mkdir simple-api
cd simple-api
Enter fullscreen mode Exit fullscreen mode

Then initialize it with npm:

npm init -y
Enter fullscreen mode Exit fullscreen mode

This creates a package.json file that holds your project information.

Step 2: Installing Express

Express is a lightweight Node.js framework that makes building APIs super easy.

Install it with:

npm install express
Enter fullscreen mode Exit fullscreen mode

After it installs, create a new file named server.js (or index.js).

Step 3: Creating a Basic Server

Now open your file and write this code:

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

app.get('/', (req, res) => {
  res.send('Welcome to my first API!');
});

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

Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

  • We import Express.

  • Create an app instance.

  • Define a route for the homepage (/).

  • Start the server on port 5000.

Run the server:

node server.js

Enter fullscreen mode Exit fullscreen mode

Then open your browser and visit http://localhost:5000
.
You should see:

“Welcome to my first API!”

🎉 Congrats! You just created your first working API endpoint

*Step 4: Adding More Routes
*

Let’s make it a little more useful.
Add a route that returns a list of users:

const users = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Smith' },
  { id: 3, name: 'Mike Johnson' }
];

app.get('/api/users', (req, res) => {
  res.json(users);
});
Enter fullscreen mode Exit fullscreen mode

Now when you visit http://localhost:5000/api/users
, you’ll see:

[
  { "id": 1, "name": "John Doe" },
  { "id": 2, "name": "Jane Smith" },
  { "id": 3, "name": "Mike Johnson" }
]

Enter fullscreen mode Exit fullscreen mode

Boom 💥 — you’ve got a working JSON API.

Step 5: Handling POST Requests
Enter fullscreen mode Exit fullscreen mode

Sometimes you want to send data to your API (like adding a new user).
For that, we’ll use POST requests.

First, tell Express to use JSON:

app.use(express.json());
Enter fullscreen mode Exit fullscreen mode

Then create a POST route:

app.post('/api/users', (req, res) => {
  const newUser = req.body;
  users.push(newUser);
  res.status(201).json(newUser);
});
Enter fullscreen mode Exit fullscreen mode

Now you can send a POST request using a tool like Postman or cURL, and it’ll add new users to the array.

Step 6: Using Route Parameters

We can also get a single user by ID:

app.get('/api/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);
});

Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:5000/api/users/1
— you’ll get:

{ "id": 1, "name": "John Doe" }

Enter fullscreen mode Exit fullscreen mode

Step 7: Finishing Touches

By now, your API can:

  • Return all users

  • Add new users

- Return a single user by ID
Enter fullscreen mode Exit fullscreen mode

You just built a functional REST API — no frameworks, no complexity.

Here’s your complete code:

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

app.use(express.json());

const users = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Smith' },
  { id: 3, name: 'Mike Johnson' }
];

app.get('/', (req, res) => {
  res.send('Welcome to my first API!');
});

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

app.get('/api/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);
});

app.post('/api/users', (req, res) => {
  const newUser = req.body;
  users.push(newUser);
  res.status(201).json(newUser);
});

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

Enter fullscreen mode Exit fullscreen mode

Building an API with Node.js and Express is one of the best ways to start learning backend development.
It teaches you how servers, routes, and HTTP methods work all using JavaScript.

Once you’re comfortable with this, you can go deeper by:

Connecting your API to a real database (MongoDB or PostgreSQL)

Adding authentication with JWT

Deploying to platforms like Render or Vercel

And if you want to keep learning web development and backend skills step by step, check out LearnWithJossy.com
where I share beginner-friendly guides on web development, Next.js, and crypto trading.

It’s the perfect place to grow your tech skills and build real-world projects with confidence. 🚀

Top comments (0)