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:
Node.js installed on your computer
A code editor like VS Code
Basic understanding of JavaScript
To check if Node.js is installed, open your terminal and run:
node -v
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
Then initialize it with npm:
npm init -y
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
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}`);
});
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
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);
});
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" }
]
Boom 💥 — you’ve got a working JSON API.
Step 5: Handling POST Requests
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());
Then create a POST route:
app.post('/api/users', (req, res) => {
const newUser = req.body;
users.push(newUser);
res.status(201).json(newUser);
});
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);
});
Visit http://localhost:5000/api/users/1
— you’ll get:
{ "id": 1, "name": "John Doe" }
Step 7: Finishing Touches
By now, your API can:
Return all users
Add new users
- Return a single user by ID
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}`);
});
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)