Back to blog
ExpressJs

Creating Routes and Handling Requests with Express

May 10, 2026 3 min read

What Is Express.js?

Express.js is a lightweight web framework for Node.js.

It simplifies:

  • Routing

  • Request handling

  • Responses

  • Middleware

  • APIs


Why Express Simplifies Node.js Development

Raw Node.js HTTP servers require more boilerplate.


Raw Node.js Server

const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.end("Home Page");
  }
});

server.listen(3000);


Express Server

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Home Page");
});

app.listen(3000);

Much cleaner and easier to read.


Install Express

npm install express


Creating Your First Express Server

const express = require("express");

const app = express();

app.listen(3000, () => {
  console.log("Server running");
});


What Is Routing?

Routing means deciding which code runs for a specific URL and HTTP method.


Example Routes

MethodRoutePurpose
GET/usersGet users
POST/usersCreate user

Request → Response Flow

Diagram: flowchart LR

Handling GET Requests

GET requests usually fetch data.


Example

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Welcome to Express");
});

app.listen(3000);


Visit

http://localhost:3000


Output

Welcome to Express


Understanding Route Handler

app.get("/", (req, res) => {
  res.send("Hello");
});


Breakdown

PartPurpose
app.get()Handle GET request
/Route path
reqIncoming request
resServer response

Express Routing Structure

Diagram: flowchart TD

Handling Multiple Routes

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Home Page");
});

app.get("/about", (req, res) => {
  res.send("About Page");
});

app.listen(3000);


Handling POST Requests

POST requests usually send data to server.

Examples:

  • Login forms

  • Registration

  • Creating records


Example

const express = require("express");

const app = express();

app.post("/users", (req, res) => {
  res.send("User created");
});

app.listen(3000);


GET vs POST

MethodPurpose
GETFetch data
POSTSend/create data

Sending Responses

Express provides helpful response methods.


Sending Text

res.send("Hello");


Sending JSON

res.json({
  name: "Ali",
  age: 22
});


Example JSON Route

app.get("/user", (req, res) => {
  res.json({
    name: "Sara",
    city: "Lucknow"
  });
});


Output

{
  "name": "Sara",
  "city": "Lucknow"
}


Route Matching Example

Diagram: sequenceDiagram

Starting the Server

app.listen(3000, () => {
  console.log("Server started");
});


Running the Application

node app.js


Common Beginner Mistakes

MistakeFix
Forgetting app.listen()Server won't start
Wrong HTTP methodUse correct route type
Missing Express installRun npm install express

Practical Example

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Home");
});

app.get("/products", (req, res) => {
  res.json([
    "Laptop",
    "Phone",
    "Keyboard"
  ]);
});

app.post("/products", (req, res) => {
  res.send("Product added");
});

app.listen(3000);


Key Takeaways

ConceptSummary
Express.jsWeb framework for Node.js
RouteURL + HTTP method
GETFetch data
POSTSend/create data
reqIncoming request
resOutgoing response

Final Notes

Express became popular because it makes backend development:

  • Faster

  • Cleaner

  • Easier to maintain

Almost all Node.js backend applications use Express or frameworks built around similar routing concepts.



0 Comments

Sign in to join the conversation

No comments yet. Be the first to comment!