Express Application Basics

Build a small Express server with middleware, routes, and a listening port.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What is Express.js? Why do we use Express? How to create an Express application Basic routing Starting a server Real-world examples Interview questions What is Express.js? Express.js is a fast, lightweight, and flexible web framework for Node.js. It helps you build: REST APIs Web Applications Backend Servers Microservices Simple Definition: Express.js is a framework that makes it easier to build web servers and APIs in Node.js. Why Do We Need Express? Without Express, creating a server using Node.js requires more code. Express provides: Easy Routing Middleware Support Request & Response Handling Error Handling REST API Development Real-Life Example 🏢 Imagine building a house. Node.js = Bricks Express.js = Ready-made House Design Node.js provides the foundation, while Express simplifies development. Install Express npm install express Import Express const express = require ( "express" ); Create an Express App const express = require ( "express" ); const app = express (); Here: express() creates an Express application. app is the main application object. Start the Server const PORT = 3000 ; app . listen( PORT , () => { console . log( `Server running on port ${ PORT } ` ); }); Output Server running on port 3000 Complete Example const express = require ( "express" ); const app = express (); app . listen( 3000 , () => { console . log( "Server Started" ); }); Basic Route app . get( "/" , ( req , res ) => { res . send( "Hello Express" ); }); Visit: http://localhost:3000 Output Hello Express Multiple Routes app . get( "/" , ( req , res ) => { res . send( "Home Page" ); }); app . get( "/about" , ( req , res ) => { res . send( "About Page" ); }); app . get( "/contact" , ( req , res ) => { res . send( "Contact Page" ); }); HTTP Methods in Express Method Purpose app.get() Read data app.post() Create data app.put() Replace data app.patch() Update data app.delete() Delete data Request & Response app . get( "/user" , ( req , res ) => { res . send( "User Details" ); }); req → Client request. res → Server response. Return JSON app . get( "/user" , ( req , res ) => { res . json({ id: 1 , name: "Ajay" }); }); Output { "id": 1 , "name": "Ajay" } Route Flow Browser │ ▼ HTTP Request │ ▼ Express Route │ ▼ Controller Logic │ ▼ Response Folder Structure project/ │ ├── node_modules/ ├── package.json ├── app.js └── routes/ Real-Life Example 🛒 Shopping Website Customer │ ▼ GET /products │ ▼ Express Server │ ▼ Database │ ▼ Product List Express Application Lifecycle Client Request │ ▼ Express App │ ▼ Route │ ▼ Business Logic │ ▼ Response Interview Question How do you create an Express application? Answer const express = require ( "express" ); const app = express (); Which method starts the Express server? Answer app . listen() Best Practices ✅ Organize routes into separate files. ✅ Return proper HTTP status codes. ✅ Use res.json() for API responses. ✅ Store the port in an environment variable. const PORT = process . env . PORT || 3000 ; ❌ Don't put all routes and business logic in one file. Key Points ✅ Express.js is a web framework for Node.js. ✅ express() creates an application instance. ✅ app.listen() starts the server. ✅ app.get() , app.post() , app.put() , app.patch() , and app.delete() define routes. ✅ req represents the incoming request, and res sends the response. ✅ Express simplifies building web servers and REST APIs. 🧠 Easy Formula Install Express │ ▼ require("express") │ ▼ const app = express() │ ▼ Create Routes │ ▼ app.listen() express() → Create App app.get() → GET Route app.post() → POST Route app.put() → PUT Route app.patch()→ PATCH Route app.delete() → DELETE Route app.listen() → Start Server Remember: 🚀 Express = Simple framework for building Node.js servers 📍 express() creates the application 🌐 Routes handle incoming requests ▶️ app.listen() starts the server