Create your first API with Express.js, the magical way
✨ Intro: From “Huh?” to Backend Hero
When I first heard of backend development, I imagined wizards casting spells in some mysterious terminal. Turns out—it’s just JavaScript + Node.js! In this guide, I’ll walk you through building your first API with Express.js, and you’ll feel like a backend wizard too 🧙♂️
🧰 What You’ll Need
- Basic knowledge of JavaScript
- Node.js installed (get it from nodejs.org)
- Any code editor (VS Code recommended)
- 15 minutes and a cup of coffee ☕
🛠 Step 1: Create the Magic Workspace
mkdir magic-api
cd magic-api
npm init -y
You just cast your first backend spell—this creates a package.json
file with default config.
✨ Step 2: Summon Express.js
Express.js is like your wand. It helps you create APIs easily.
npm install express
🔮 Step 3: Create Your Spellbook (index.js
)
In your magic-api
folder, create a file called index.js
and add:
const express = require('express');
const app = express();
const PORT = 3000;
// Spell #1: Basic Hello API
app.get('/', (req, res) => {
res.send('✨ Welcome to your magical API!');
});
// Spell #2: Return wizard data
app.get('/wizard', (req, res) => {
res.json({
name: "Devesh the Backend Wizard",
specialty: "Building APIs with Node.js & Express",
spells: ["GET", "POST", "PUT", "DELETE"]
});
});
app.listen(PORT, () => {
console.log(`🧙 Server running at http://localhost:${PORT}`);
});
🚀 Step 4: Launch Your Wizardry
In your terminal:
node index.js
Now visit http://localhost:3000 in your browser.
You’ll see: ✨ Welcome to your magical API!
Try http://localhost:3000/wizard to see your wizard profile in JSON form!
🧪 Bonus: Add a POST Spell
app.use(express.json());
app.post('/spellbook', (req, res) => {
const { spellName } = req.body;
res.send(`🌀 Spell "${spellName}" has been added to your spellbook!`);
});
Now test it in Postman or a REST client:
POST http://localhost:3000/spellbook
Body (JSON):
{
“spellName”: “fireball”
}
📚 What You Just Learned
✅ How to create a basic Express.js API
✅ How to return plain text and JSON
✅ How to handle POST requests with JSON body
✅ You’re officially casting backend spells with Node.js!
🎁 Final Tips
- Want to save your spells? We’ll add a database in the next post!
- Want to build a spellbook manager app? You’re halfway there.
- Don’t stop here—APIs are the gateway to the backend kingdom. Keep going!