🧪 Building RESTful APIs with Node.js and Express: The Heisenberg Method

Muthu YuvarajMuthu Yuvaraj
4 min read

This blog uses Breaking Bad references, storytelling, and backend dev concepts — all cooked into one explosive mix. Let's turn your blog post into an episode of a dev-turned-code kingpin, learning to cook clean APIs the Heisenberg way.


“I am not in danger, Skyler. I am the server.”
— Walter White (Probably, if he built REST APIs)


👨‍🔬 Episode 1: The Chemistry of APIs

It all started with a need.
A need to understand how data flows, how clients talk to servers, and how clean architecture matters — like pure blue crystal.

Like Walter’s descent into the underworld of meth labs, I stepped into the backend world, only instead of RVs and gas masks, I had:

  • Node.js 🟢

  • Express 🚀

  • MongoDB 🍃

  • Postman 🧪

This was my first cook — the API cook.


🧪 Episode 2: Say My Name — The Setup

You clearly don’t know who you’re talking to. I’m the one who installs.”

📦 Setting Up the Lab

bashCopyEditmkdir blue-api
cd blue-api
npm init -y
npm install express mongoose

🌡️ Babel? Call it the Lab Upgrade

bashCopyEditnpm install --save-dev @babel/core @babel/cli @babel/preset-env

.babelrc:

jsonCopyEdit{ "presets": ["@babel/preset-env"] }

Like the makeshift RV lab, this setup might seem small — but trust me, it can blow up into something huge.


🛠️ Episode 3: The First Cook — Server Boot

Walter didn’t start with a perfect batch. Neither will you.

Here’s how I fired up my first batch — a basic API server.

jsCopyEditimport express from 'express';
const app = express();
app.use(express.json());

app.get('/', (req, res) => {
  res.send('Say my name... it’s the API');
});

app.listen(3000, () => {
  console.log('💥 Server cooking on port 3000');
});

🧱 Episode 4: The Cartel — Enter MongoDB

Jesse, we need storage.”

🔗 Connecting to the Database Lab

jsCopyEditimport mongoose from 'mongoose';

mongoose.connect(process.env.MONGO_URI)
  .then(() => console.log('📡 Connected to the cartel (MongoDB)'))
  .catch(err => console.error('⛔ DB down, Heisenberg not happy'));

🧬 Designing the Formula

jsCopyEditimport mongoose from 'mongoose';

const batchSchema = new mongoose.Schema({
  purity: String,
  cookedBy: String,
  timestamp: { type: Date, default: Date.now }
});

export default mongoose.model('Batch', batchSchema);

🔥 Episode 5: Full Cook — CRUD Operations

Just like Walt’s product had stages — cook, refine, package, distribute — so does our API.


🧾 Create a Batch (POST)

jsCopyEditapp.post('/batches', async (req, res) => {
  const batch = new Batch(req.body);
  await batch.save();
  res.status(201).send(batch);
});

📦 Read All Batches (GET)

jsCopyEditapp.get('/batches', async (req, res) => {
  const all = await Batch.find();
  res.send(all);
});

🔍 Read by ID (GET)

jsCopyEditapp.get('/batches/:id', async (req, res) => {
  const batch = await Batch.findById(req.params.id);
  res.send(batch);
});

🔁 Update the Batch (PUT)

jsCopyEditapp.put('/batches/:id', async (req, res) => {
  const updated = await Batch.findByIdAndUpdate(req.params.id, req.body, { new: true });
  res.send(updated);
});

🗑️ Delete the Evidence (DELETE)

jsCopyEditapp.delete('/batches/:id', async (req, res) => {
  await Batch.findByIdAndDelete(req.params.id);
  res.send({ message: 'Evidence destroyed. Heisenberg approves.' });
});

🎭 Episode 6: Saul Goodman’s Tips (Middleware & Tools)

“You need a lawyer. And also a logger.” — Saul Goodman

🕵️ Request Logger Middleware

jsCopyEditapp.use((req, res, next) => {
  console.log(`[${req.method}] - ${req.url}`);
  next();
});

🧰 Other Tools in the Lab

  • dotenv — keep secrets hidden like Walt’s money

  • cors — allow trusted access to your lab

  • morgan — logs every visitor, even Hank Schrader 🕵️‍♂️


📁 Episode 7: Hiding the Lab — Static Files

Sometimes, you need to serve more than just API routes.

jsCopyEditapp.use('/lab', express.static('public'));

Now your “public” folder becomes your frontend drop point — like Walt’s underground lab.


📺 Finale: I Am the API

From a humble setup to a fully functional, CRUD-powered API — this was my origin story as a backend developer.

“You know exactly who built this API. Say my name.”
“It’s... Heisenberg.”
“You’re goddamn right.”


🔄 What’s Next in the Breaking Backend Universe?

  • Hosting the API on Render or Railway

  • Adding JWT Auth (like hiding your real identity 👨‍🔬)

  • Consuming it from a React frontend

  • Error-handling like Gus Fring — calm, deadly, precise

💬 Leave a comment, Jesse.
💎 Follow for more clean backend cooking.

0
Subscribe to my newsletter

Read articles from Muthu Yuvaraj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Muthu Yuvaraj
Muthu Yuvaraj