DEV Community

Cover image for Day 23 of #100DaysOfCode — Introduction to Express.js
M Saad Ahmad
M Saad Ahmad

Posted on

Day 23 of #100DaysOfCode — Introduction to Express.js

Today marks Day 23 of my 100 Days of Code journey, where I immerse myself in Express.js. For anyone dedicated to backend development with JavaScript, Express.js is an essential tool, and my experience today has underscored its significance.


🌟 What Is Express.js and Why Do We Use It?

Express.js is a lightweight web framework for Node.js that helps you build web servers and APIs much faster, with way less boilerplate.

Here’s a simple analogy:

  • Node.js = raw ingredients (flour, eggs, sugar)
  • Express.js = a ready-made cake mix

Sure, you can bake a cake from scratch… but using a mix saves time, effort, and prevents mistakes — while still producing the same result.

Node.js gives you the power, but Express saves you a ton of repetitive work.


Without Express: Pure Node.js Is… a Lot of Work 😭

When you build a server using plain Node.js, you must:

  • Manually check the URL for routing
  • Manually parse incoming request data
  • Manually set response headers
  • Manually handle errors and 404 routes

Here’s what simple routing looks like in plain Node.js:

const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/home' && req.method === 'GET') {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Welcome to Home Page');

  } else if (req.url === '/api/users' && req.method === 'POST') {

    let body = '';
    req.on('data', chunk => (body += chunk.toString()));
    req.on('end', () => {
      const parsedData = JSON.parse(body);
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ message: 'User received', user: parsedData }));
    });

  } else {
    res.writeHead(404);
    res.end('Not Found');
  }
});

server.listen(3000);
Enter fullscreen mode Exit fullscreen mode

This is fine for tiny apps, but imagine doing this for 20, 50, or 100 routes — it quickly becomes painful.

Problems with pure Node.js:

  • Messy if/else chains when routes increase
  • Manual parsing using data chunks
  • Re-writing headers and responses again and again
  • Harder to maintain and scale

With Express.js: Clean and Readable 🌟

With Express, the same logic becomes super clean:

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

app.use(express.json()); // Automatic JSON parsing

app.get('/home', (req, res) => {
  res.send('Welcome to Home Page');
});

app.post('/api/users', (req, res) => {
  const user = req.body;
  res.json({ message: 'User received', user });
});

app.use((req, res) => {
  res.status(404).send('Not Found');
});

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

No manual routing
No manual parsing
No manual headers
Cleaner code, fewer bugs, faster development


🚗 Node.js vs Express.js

Think of Node.js as a manual car.
You learn the gears, clutch, and engine operations — useful for learning, but exhausting long-term.

Express.js is like an automatic car.
All the heavy lifting (like gear shifting) is handled for you so you can focus on driving, i.e., building your project.


🧭 What Is Routing?

Routing decides “What should the server do based on the URL a user visits?”

Examples:

  • /home → show home page
  • /about → show about page
  • /api/users → return user list

Routing is one of Express’s strongest features, allowing clean and organized structure using:

app.get()
app.post()
app.put()
app.delete()
Enter fullscreen mode Exit fullscreen mode

Much cleaner than manual if/else chains.


📦 What Is Parsing?

Whenever a user submits data (forms, JSON, etc.), it arrives as raw text or bytes.

Example request body:

{"name":"John","age":25}
Enter fullscreen mode Exit fullscreen mode

You don’t want to manually convert this to an object every time.

Express handles it automatically using:

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

Now you can instantly use:

req.body.name
req.body.age
Enter fullscreen mode Exit fullscreen mode

📞 What Are Request & Response?

Think of it as ordering food:

  • Request → You say: “I want a pizza.”
  • Response → Restaurant brings the pizza.

In technical terms:

Request contains:

  • URL
  • Method (GET, POST, PUT, DELETE)
  • Headers
  • Body data

Response contains:

  • Data you send back (HTML, JSON, text)
  • Status codes
  • Headers

Express simplifies all of this through helper functions like res.send() and res.json().


🔐 What Is Middleware?

Middleware is simply a function that runs between the request and the response.

Example:

🧑 Request
→ 🔦 Security Check
→ 🎟 Ticket Check
→ 🎉 Route Handler (Final Response)

Every request passes through these layers. Middleware is perfect for:

  • Authentication
  • Logging
  • Parsing
  • Error handling
  • Validations

Express lets you plug in middleware anywhere — making it extremely powerful.


🔍 Side-by-Side Comparison

Task Raw Node.js Express.js
Routing Manual if/else app.get(), app.post(), etc.
Parsing Body Collect chunks + JSON.parse() app.use(express.json())
Sending Response res.writeHead() + res.end() res.send() / res.json()
404 Handler Manual else block app.use()

🧩 The Most Common Express.js Functions You’ll Use

Function Purpose
app.get/post/put/delete Handle specific request types
app.use() Run middleware on every request
req.params Read URL parameters like /users/:id
req.query Read URL queries like ?q=hello
req.body Read submitted data
res.send() Send text/HTML
res.json() Send JSON
res.status() Set status code
express.Router() Modular routing
app.listen() Start server

🎯 Final Thoughts — Why Express.js Matters

You can build servers with raw Node.js. But Express gives you:

  • Clean code
  • Less repetition
  • Easy routing
  • Automatic parsing
  • Middleware power
  • Flexibility to scale

For backend developers, Express.js isn’t just convenient — it’s practically essential.


✍️ Conclusion

Today’s lesson made one thing very clear:

Node.js gives you power. Express.js gives you productivity.

Express.js removes the repetitive work of building servers and lets you focus on logic instead of boilerplate. No wonder it’s the most popular backend framework in the JavaScript ecosystem.

This wraps up my Day 23 of the 100 Days of Code challenge — and Express.js definitely feels like a game-changer.

Happy coding!

Top comments (0)