Skip to main content

Command Palette

Search for a command to run...

Creating Routes and Handling Requests with Express

Updated
3 min read
Creating Routes and Handling Requests with Express
S

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.

When working with Node.js, writing everything using the built-in http module can quickly become complex. This is where Express.js comes in. It simplifies backend development and makes handling routes and requests much easier.

Let’s understand this step by step.


1. What Express.js is

Express.js is a minimal and flexible web framework for Node.js.

It helps you:

  • Create servers easily

  • Handle routes (URLs)

  • Manage requests and responses

  • Build APIs quickly

Instead of writing long and repetitive code using Node’s http module, Express gives you simple methods to handle everything.


2. Why Express Simplifies Node.js Development

Without Express, creating a server looks like this:

const http = require('http');

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

server.listen(3000);

As your application grows, this becomes hard to manage.

With Express, the same thing becomes:

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

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

app.listen(3000);

Express provides:

  • Cleaner syntax

  • Easy routing

  • Built-in middleware support

  • Better organization of code


3. Creating Your First Express Server

First, install Express:

npm init -y
npm install express

Now create a file app.js:

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

// basic route
app.get('/', (req, res) => {
  res.send('Welcome to Express Server');
});

// start server
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Run the server:

node app.js

Open browser:

http://localhost:3000

You will see your response.


4. Handling GET Requests

GET requests are used to fetch data from the server.

Example:

app.get('/users', (req, res) => {
  res.send('List of users');
});

You can also access query data:

app.get('/search', (req, res) => {
  const keyword = req.query.keyword;
  res.send(`Searching for ${keyword}`);
});

5. Handling POST Requests

POST requests are used to send data to the server.

Before handling POST data, remember to enable JSON parsing

app.use(express.json());

Example:

app.post('/users', (req, res) => {
  const user = req.body;
  res.send(`User received: ${JSON.stringify(user)}`);
});

You can test POST requests using tools like Postman or frontend forms.


6. Sending Responses

Express provides different methods to send responses:

Send text:

res.send('Hello World');

Send JSON:

res.json({ message: 'Success' });

Send status:

res.status(200).send('OK');

Send file:

res.sendFile(__dirname + '/index.html');

These methods make it easy to control what the client receives.


Final Understanding

  • Express.js is a lightweight framework built on Node.js

  • It simplifies server creation and routing

  • You can easily handle GET and POST requests

  • Request data can be accessed using req

  • Responses are sent using res methods

  • It helps organize backend code in a clean and scalable way


Summary

Express.js removes much of the complexity involved in using Node.js directly. Instead of manually handling request URLs and responses, you define clear routes using simple methods like app.get() and app.post(). This makes your code easier to read, maintain, and scale. By learning how to create routes and handle requests in Express, you take an important step toward building real-world backend applications such as REST APIs, authentication systems, and full-stack projects.