VOOZH about

URL: https://www.geeksforgeeks.org/web-tech/introduction-to-express/

⇱ Introduction to Express - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Express

Last Updated : 25 Apr, 2026

Express.js is a minimal and flexible Node.js web application framework that simplifies building server-side applications and APIs.

👁 express2
  • Middleware-based: Uses middleware to handle requests, responses, and common features.
  • Routing system: Structure—developers can design apps as they want.
  • Integration: Works well with databases (MongoDB, MySQL, PostgreSQL) and front-end frameworks.
  • Scalable: Suitable for small apps to large-scale enterprise APIs.

"Hello World" Example

Create a simple "Hello World" application with Express.

Start the Server:

Run your server with the following command

node server.js

Web Browser Output

When you navigate to http://localhost:3000 in your web browser, you should see:

👁 HelloGeeks-

Working of Express

Express.js works by handling client requests through a series of steps called the request-response cycle, using middleware and routing to process requests and send responses efficiently.

👁 workingofexpress
  • A client sends a request (e.g., browser or app) to the Express server.
  • The request flows through middleware that handles logging, authentication, and other processing tasks.
  • Express matches the request to a route handler based on the URL and HTTP method.
  • The route handler processes the request and prepares a response.
  • The server sends the response back to the client.
  • If any error occurs, it is caught by error-handling middleware to ensure the server remains stable.

Basic Routing in Express.js

Basic routing in Express.js is the process of defining URL endpoints (routes) and specifying how the server should respond to client requests at those endpoints.

Express provides simple methods to define routes that correspond to HTTP methods:

  • app.get() - Handle GET requests.
  • app.post() - Handle POST requests.
  • app.put() - Handle PUT requests.
  • app.delete() - Handle DELETE requests.
  • app.all() - Handle all HTTP methods.
  • The server handles different HTTP requests using routes (GET, POST, PUT, DELETE, ALL).
  • Each route sends a response when its specific URL is accessed.
  • The program does not block and can handle multiple requests efficiently.
  • The server starts and listens on port 3000.

Parameters in Express.js

Parameters in Express.js are values that are passed in the URL or request that your server can use to process the request dynamically. They allow your routes to handle different inputs without creating multiple static routes.

Types of Parameters in Express:

1. Route Parameters: Part of the URL (e.g., /users/:id) accessed via req.params.

2. Query Parameters: Appended to the URL (e.g., /search?term=nodejs) accessed via req.query.

3. Request Body Parameters: Sent in POST or PUT requests, accessed via req.body with express.json() middleware.

Middleware in Express.js

Middleware in Express.js is a function that executes during the request-response cycle. It has access to the request (req) and response (res) objects, and can either end the request-response process or pass control to the next middleware using next().

Middleware is used for

  • Logging requests
  • Authentication and authorization
  • Parsing request bodies
  • Handling errors

Built-in Middleware

Express includes several useful middleware functions:

  • express.json() - Parse JSON request bodies
  • express.urlencoded() - Parse URL-encoded request bodies
  • express.static() - Serve static files
  • express.Router() - Create modular route handlers
  • Middleware logs each request method and URL before handling it.
  • next() passes control to the next middleware or route.
  • JSON middleware parses incoming request data.
  • The route / sends a response to the client.
  • The server runs on port 3000.

Error Handling in Express.js

Error handling in Express is done through special middleware functions that have four arguments:

(err, req, res, next).

  • The /error route creates and passes an error using next(err).
  • Error-handling middleware catches and handles the error.
  • The error is logged using console.error().
  • A response with error message and status is sent to the client.
  • The server runs on port 3000.

Usage of Express.js

Express.js is a popular web framework for Node.js that simplifies server-side development. It is widely used to build web applications, APIs, and scalable server-side solutions.

  • Web Applications: Build dynamic sites and SPAs efficiently.
  • RESTful APIs: Create APIs for CRUD operations and data delivery.
  • Middleware Apps: Add logging, authentication, and validation.
  • Microservices: Lightweight and modular for scalable services.
  • Server-Side Rendering: Supports EJS, Pug, Handlebars.
  • Proxy/Gateway: Routes and manages HTTP traffic.
  • Rapid Prototyping: Quickly develop and test server logic.

Also Check

Comment