codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

20 / 63 topics
19Middleware20Express Framework21Routing22Templates23Handling Forms24Sessions25Authentication26API Development
Tutorials/Node.js/Express Framework
🟢Node.js

Express Framework

Updated 2026-04-20
3 min read

Express Framework

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It simplifies the process of building server-side applications by providing tools, libraries, and middleware to handle requests, routes, and responses efficiently.

Introduction to Express

Express is built on top of the Node.js HTTP module and makes it easier to manage server-side logic. It allows developers to create a web server using fewer lines of code compared to handling HTTP requests directly with Node.js. Express supports various features like routing, middleware, templating engines, and error-handling mechanisms.

Setting Up Express

To start using Express in your Node.js application, you need to install it first. You can do this using npm (Node Package Manager).

npm install express

Once installed, you can require Express in your JavaScript file and create an instance of the express object.

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

Basic Server Setup

Here is a basic example of setting up a server using Express:

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

// Define a port number
const PORT = 3000;

// Create a route for the root URL
app.get('/', (req, res) => {
    res.send('Hello World!');
});

// Start the server
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

In this example:

  • We import Express and create an app instance.
  • We define a port number where our server will listen for incoming requests.
  • We set up a route for the root URL (/) that sends a "Hello World!" response when accessed.
  • Finally, we start the server using app.listen().

Routing

Routing is a crucial part of any web application. Express provides a simple way to define routes using methods like get, post, put, and delete.

// Define routes for different HTTP methods
app.get('/users', (req, res) => {
    res.send('List of users');
});

app.post('/users', (req, res) => {
    res.send('User created');
});

app.put('/users/:id', (req, res) => {
    res.send(`User ${req.params.id} updated`);
});

app.delete('/users/:id', (req, res) => {
    res.send(`User ${req.params.id} deleted`);
});

In this example:

  • We define routes for different HTTP methods (GET, POST, PUT, DELETE).
  • The :id in the route /users/:id is a route parameter that captures part of the URL.

Middleware

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. These functions can execute any code, make changes to the request and the response objects, end the request-response cycle, and call the next middleware function.

// Middleware to log requests
app.use((req, res, next) => {
    console.log(`${req.method} ${req.url}`);
    next();
});

// Route that uses the middleware
app.get('/about', (req, res) => {
    res.send('About page');
});

In this example:

  • We define a middleware function that logs the HTTP method and URL of each request.
  • The next() function is called to pass control to the next middleware function.

Error Handling

Express provides a way to handle errors using error-handling middleware. This middleware has four arguments: err, req, res, and next.

// Route that throws an error
app.get('/error', (req, res) => {
    throw new Error('Something went wrong!');
});

// Error-handling middleware
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

In this example:

  • We define a route that throws an error.
  • The error-handling middleware catches the error and sends a 500 status code with a message.

Templating Engines

Express supports various templating engines like EJS, Pug, and Handlebars. These engines allow you to generate HTML dynamically using templates.

npm install ejs
// Set the view engine to EJS
app.set('view engine', 'ejs');

// Define a route that renders an EJS template
app.get('/profile', (req, res) => {
    const user = { name: 'John Doe' };
    res.render('profile', { user });
});

In this example:

  • We set the view engine to EJS.
  • We define a route that renders an ejs template (profile.ejs) and passes a user object to it.

Best Practices

  1. Use Middleware for Reusability: Instead of writing the same logic in multiple routes, use middleware functions to handle common tasks like authentication, logging, etc.
  2. Keep Routes Organized: For larger applications, organize your routes into separate files or modules to keep your code clean and maintainable.
  3. Handle Errors Gracefully: Always include error-handling middleware to catch and log errors, and send appropriate responses to the client.

Conclusion

Express is a powerful tool for building web applications in Node.js. It simplifies the process of handling HTTP requests and provides a flexible framework for adding various features like routing, middleware, templating engines, and error handling. By following best practices and leveraging the full potential of Express, you can build robust and scalable web applications efficiently.

Additional Resources

  • Express Official Documentation
  • Node.js Official Website
  • EJS Templating Engine

PreviousMiddlewareNext Routing

Recommended Gear

MiddlewareRouting