DEV Community

Shivam Yadav
Shivam Yadav

Posted on

Creating Routes and Handling Requests with Express

Creating Routes and Handling Requests with Express

If raw Node.js feels like:

building furniture using individual pieces of wood,

then Express.js feels like:

finally discovering screws, tools, and basic human happiness.

Because let’s be honest.

Creating servers using pure Node.js is great for learning.

But after writing this for the 17th time:

```js id="a82ks1"
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello World");




you start questioning your life choices.

That is exactly why Express.js became popular.

It removes unnecessary pain and lets developers focus on:

* routes
* logic
* APIs
* applications

instead of wrestling with low-level server code every five minutes.

In this article, we will learn:

* What Express.js is
* Why developers use it
* How to create an Express server
* Handling GET requests
* Handling POST requests
* Sending responses properly
* Understanding routing clearly

And yes, we are keeping things beginner-friendly without turning the article into a 900-page encyclopedia.

---

# First Understand: What Is Express.js?

Express.js is a backend framework for Node.js.

It helps simplify:

* server creation
* routing
* request handling
* middleware
* APIs

Instead of writing huge amounts of raw Node.js code,
Express gives you clean and readable methods.

Think of Node.js as:

> the engine.

And Express as:

> the comfortable car built on top of it.

---

# Why Express.js Was Needed

Let’s compare.

---

# Creating Server in Raw Node.js



```js id="f71sk2"
const http = require("http");

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

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

This works.

But as your application grows:

  • more routes
  • more conditions
  • more headers
  • more parsing

everything becomes messy very quickly.

Soon your file starts looking like:

ancient scrolls written by exhausted developers.


Same Thing Using Express

```js id="m92ks1"
const express = require("express");

const app = express();

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

app.listen(3000);




Cleaner.
Readable.
Less pain.
Less unnecessary code.

This is why developers love Express.

---

# Installing Express

First create a project folder.



```text id="z81sk2"
express-app
Enter fullscreen mode Exit fullscreen mode

Open terminal inside it.

Now initialize Node project:

```bash id="x71ps2"
npm init -y




This creates:



```text id="q61nd2"
package.json
Enter fullscreen mode Exit fullscreen mode

This file stores:

  • project information
  • dependencies
  • scripts

Now install Express:

```bash id="w82js1"
npm install express




Done.

Express is now added to your project.

---

# Creating Your First Express Server

Create file:



```text id="r81dk2"
app.js
Enter fullscreen mode Exit fullscreen mode

Write:

```js id="p72ms1"
const express = require("express");

const app = express();

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




Run server:



```bash id="s91kd2"
node app.js
Enter fullscreen mode Exit fullscreen mode

Output:

```text id="d82js1"
Server running on port 3000




Congratulations.

You now have an Express server running.

Tiny file.
Massive upgrade.

---

# Understanding What Is Happening

---

## Step 1: Import Express



```js id="e91ks2"
const express = require("express");
Enter fullscreen mode Exit fullscreen mode

Imports Express library.


Step 2: Create App

```js id="u82ms1"
const app = express();




Creates Express application instance.

This `app` object controls:

* routes
* requests
* responses
* middleware
* server behavior

---

## Step 3: Start Server



```js id="l73ps1"
app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Starts server on port 3000.


What Are Routes?

Routes are basically:

paths users visit.

Example:

```text id="g92ks1"
/




means homepage.



```text id="h83jd1"
/about
Enter fullscreen mode Exit fullscreen mode

means about page.

```text id="t72ls1"
/contact




means contact page.

Every route can perform different actions.

---

# Understanding Routing Properly

Imagine your server is a hotel receptionist.

Different customers ask for different rooms.

Example:



```text id="j82ls1"
/login
/products
/profile
/orders
Enter fullscreen mode Exit fullscreen mode

The receptionist checks:

  • which path user requested
  • then sends them to correct place

That is routing.


Handling GET Requests

GET requests are used to:

  • fetch data
  • request pages
  • retrieve information

Example:
Opening a website in browser usually sends a GET request.


First GET Route

Update app.js:

```js id="v73ms2"
const express = require("express");

const app = express();

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

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




Now visit:



```text id="o82jd1"
http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

You will see:

```text id="i72ks1"
Welcome to Homepage




---

# Another Route Example



```js id="c91ps2"
app.get("/about", (req, res) => {
    res.send("About Page");
});
Enter fullscreen mode Exit fullscreen mode

Now:

```text id="f82ls1"
http://localhost:3000/about




returns:



```text id="b71kd2"
About Page
Enter fullscreen mode Exit fullscreen mode

Different routes.
Different responses.

Simple.


Request → Route → Response Flow

```text id="n92js1"
Client Request

Express Route Matches URL

Route Handler Executes

Response Sent Back




This is the foundation of backend APIs.

---

# Understanding Route Handlers

This part:



```js id="m81ps2"
(req, res) => {

}
Enter fullscreen mode Exit fullscreen mode

is called:

route handler

It handles:

  • incoming request
  • outgoing response

What Is req?

req means:

request object

It contains:

  • URL
  • headers
  • query parameters
  • body data
  • client information

Basically:

everything user sends.


What Is res?

res means:

response object

It helps send data back to client.

Examples:

```js id="x82ks1"
res.send()
res.json()
res.status()




---

# Sending Responses

---

## Sending Text



```js id="q71ms2"
res.send("Hello");
Enter fullscreen mode Exit fullscreen mode

Sending HTML

```js id="k82ls1"
res.send("

Hello

");



---

## Sending JSON



```js id="t91jd2"
res.json({
    name: "Shivam",
    role: "Developer"
});
Enter fullscreen mode Exit fullscreen mode

Express automatically converts it into JSON response.

Very useful for APIs.


Handling POST Requests

POST requests are used to:

  • send data
  • create resources
  • submit forms

Examples:

  • login forms
  • signup forms
  • uploading data

Basic POST Route

```js id="a73ks2"
app.post("/login", (req, res) => {
res.send("Login Successful");
});




This route responds only to:

# POST requests

Not GET requests.

---

# Difference Between GET and POST

| Method | Purpose          |
| ------ | ---------------- |
| GET    | Fetch data       |
| POST   | Send/Create data |

---

# Important Beginner Confusion

If you open:



```text id="r82ks1"
http://localhost:3000/login
Enter fullscreen mode Exit fullscreen mode

in browser,
it sends:

GET request

But your route expects:

POST request

Result?

```text id="w91js2"
Cannot GET /login




This is normal.

Not a bug.

Your route simply does not handle GET there.

---

# Express Routing Structure Visualization



```text id="u73ls1"
app.get("/")        → Homepage
app.get("/about")  → About Page
app.post("/login") → Login Logic
app.get("/users")  → User Data
Enter fullscreen mode Exit fullscreen mode

Each route has:

  • method
  • path
  • handler

Why Express Feels So Powerful

Because Express removes repetitive low-level work.

Without Express:

  • manual routing
  • manual headers
  • manual parsing
  • manual response handling

With Express:

  • cleaner syntax
  • organized routes
  • faster development

It lets developers focus on:

application logic instead of server headaches.


Real-World Example

Imagine building:

  • Instagram
  • YouTube
  • Swiggy
  • Amazon

Every action becomes a route.

Example:

```text id="y82ks1"
GET /products
POST /orders
GET /profile
POST /login
DELETE /cart




Backend development is basically:

> handling requests properly and sending useful responses.

That is why routing is one of the most important concepts in Express.

---

# Common Beginner Mistakes

---

## Forgetting to Install Express

Error:



```text id="p71js2"
Cannot find module 'express'
Enter fullscreen mode Exit fullscreen mode

Solution:

```bash id="z82ls1"
npm install express




---

## Port Already Running

Error:



```text id="v91kd2"
EADDRINUSE
Enter fullscreen mode Exit fullscreen mode

Means:
another app already uses that port.

Change port:

```js id="h73ps2"
app.listen(5000)




---

## Wrong HTTP Method

Using:



```js id="n82ks1"
app.post()
Enter fullscreen mode Exit fullscreen mode

but testing in browser.

Browser sends GET request.

Result:
route not found.


Quick Revision

Express.js Is:

  • Node.js framework
  • simplifies backend development
  • handles routes easily

Install Express

```bash id="l91ms2"
npm install express




---

## Create Server



```js id="c82ls2"
const app = express();
Enter fullscreen mode Exit fullscreen mode

Start Server

```js id="s73jd1"
app.listen(3000)




---

## GET Request



```js id="d82ks1"
app.get()
Enter fullscreen mode Exit fullscreen mode

Used for fetching data.


POST Request

```js id="g91ps2"
app.post()




Used for sending data.

---

# Final Thoughts

Learning Express for the first time feels refreshing because suddenly:

* routing becomes readable
* servers become easier
* backend code stops looking terrifying

And honestly,
that is why Express became one of the most loved Node.js frameworks ever.

It takes complicated backend concepts and says:

> “Relax. We can make this cleaner.”

And that simplicity is exactly what made Express dominate backend development for years.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)