Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰
Express.jsπŸš‚ Introduction to Express.js

πŸš‚ Introduction to Express.js

Express.js is a minimal, fast, and unopinionated web framework for Node.js. It’s the de facto standard for building backend services and APIs with Node.js. It doesn’t impose a strict structure on you, giving you the flexibility to organize your application as you see fit.

  • Minimal: It provides a thin layer of fundamental web application features without obscuring the Node.js features you already know.

  • Unopinionated: It doesn’t force you to structure your application in a specific way. It gives you the freedom and flexibility to organize your code as you see fit.

Think of it this way: the built-in http module in Node.js is like a box of raw engine parts. You can build a car with it, but you’ll have to craft every single piece, from the engine block to the bolts. Express.js is like a well-designed car chassis: it provides the essential structureβ€”the frame, wheels, and steeringβ€”allowing you to focus on building the powerful engine and custom features for your application without worrying about the low-level mechanics.

It’s designed to solve common web development challenges, such as:

  • Routing: Mapping incoming request URLs to the correct handler functions.
  • Middleware: Creating a pipeline to process requests sequentially.
  • Request & Response Handling: Simplifying the process of reading request data and sending back responses.

✨ Core Features at a Glance

For an advanced developer, understanding these core pillars is key to leveraging Express effectively.

  • Middleware Architecture: This is the heart and soul of Express. A request in an Express app flows through a chain of middleware functions. Each function has access to the request (req) and response (res) objects and can perform tasks, modify the objects, or pass control to the next middleware in the chain. This makes code highly modular and reusable.
  • Powerful Routing System: Express provides a simple yet robust API to define how your application responds to client requests based on the URL path and HTTP method (e.g., app.get(), app.post()).
  • HTTP Helpers: It adds numerous convenience methods to the req and res objects (res.json(), res.status(), req.params, req.body) that abstract away the boilerplate code needed with the native http module.
  • Templating and View Engine Integration: While often used for APIs, Express can easily integrate with templating engines like Pug, EJS, or Handlebars to render dynamic HTML pages on the server.

πŸ› οΈ Installation and β€œHello World” Guide

Let’s get a basic Express server up and running.

Prerequisites

Make sure you have Node.js and npm (Node Package Manager) installed on your machine. You can verify this by running node -v and npm -v in your terminal.

Initialize Your Project

First, create a new directory for your project and navigate into it. Then, initialize a Node.js project using npm.

mkdir my-express-app cd my-express-app npm init -y

Install Express

Now, install Express as a project dependency by running the following command:

npm install express

Create Your Server File

Create a file named index.js (or app.js) and add the following β€œHello World” code.

index.js
// 1. Import the express library const express = require("express"); // 2. Instantiate the app const app = express(); // 3. Define the port the server will run on const port = 3000; // 4. Define a basic route for the root URL // This handles GET requests to http://localhost:3000/ app.get("/", (req, res) => { res.send("Hello, Express Developer!"); }); // 5. Start the server and listen for incoming connections app.listen(port, () => { console.log(`πŸš€ Server is running at http://localhost:${port}`); });

Run the Server

Execute the file using Node from your terminal.

node --watch index.js

Test Your Server

Open your web browser or a tool like Postman, and navigate to http://localhost:3000/. You should see the message β€œHello, Express Developer!” displayed in your browser.

Last updated on