Open In App

Unique features of Express JS

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Express.js is a lightweight and powerful web framework built for Node.js. It simplifies building web applications and APIs by providing essential features that help developers create scalable, high-performance applications easily. Let's explore the unique features of Express.js that make it a popular choice for web development.

1. Minimalist Framework

Express.js is lightweight and focuses on only the essential features needed to create a web server or API.

You can get started with Express in just a few lines of code, without unnecessary complexity. It helps you set up routes, handle requests, and send responses quickly.

JavaScript
//app.js
const express = require('express');
const app = express();

// Define a simple route
app.get('/', (req, res) => {
  res.send('Hello, Express!');
});

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

Output

Screenshot-2025-03-05-142223
Minimalist Framework
  • Minimal Setup: Just a few lines of code to define a route and start the server.
  • Route: The app.get() method defines a route that responds with "Hello, Express!" when the home page (/) is accessed.
  • Server Initialization: The app.listen() method starts the server on port 3000.

2. Powerful Routing System

Express.js provides a powerful routing system that allows you to define routes for different HTTP methods (GET, POST, PUT, DELETE) and URLs. This makes it flexible for handling various requests.

JavaScript
const express = require('express');
const app = express();
// GET request route
app.get('/about', (req, res) => {
    res.send('About us');
  });
  // POST request route
  app.post('/submit', (req, res) => {
    res.send('Form submitted');
  });
// Start the server
app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});

Output:

GET (/about) Route:

Screenshot-2025-03-05-143047
/about route

Post(/submit) Route:

Screenshot-2025-03-05-143404
/submit route
  • Define a GET route: app.get('/about', (req, res) => { res.send('About us'); }); sets up a route for /about that responds with "About us" when accessed via GET request.
  • Define a POST route: app.post('/submit', (req, res) => { res.send('Form submitted'); }); sets up a route for /submit that responds with "Form submitted" when a POST request is made.
  • Start the server: app.listen(3000, () => { console.log('Server is running on http://localhost:3000'); }); starts the server on port 3000 and logs a message when the server is ready.

3. Middleware Support

Express.js allows you to use middleware functions to process requests and responses. Middleware can be applied globally or to specific routes, helping organize your application logic.

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

// Example of a middleware function
app.use((req, res, next) => {
    console.log('Request received at:', Date.now());
    next();  // Pass control to the next middleware or route
  });
  
  // Sample route
  app.get('/home', (req, res) => {
    res.send('Welcome to the home page!');
  });
  
  

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

Output

Screenshot-2025-03-05-144047
middleware function execution
Screenshot-2025-03-05-144040
/home route
  • Middleware Function: Logs the current time whenever a request is received, then passes control to the next step.
  • Route Handling: Defines a route (/home) that sends a welcome message when accessed.
  • Server Initialization: Starts the server on port 3000 and logs a message indicating the server is running.

4. Templating Engines

Express allows you to use various templating engines like Pug, EJS, or Handlebars. Here's an example using EJS to render dynamic content.

  • First, install the ejs package:
npm install ejs
  • Now, set up Express to use EJS and render a dynamic template and also make a folder by the name views and then make a profile.ejs file in it
JavaScript
//profile.ejs
<h1>Profile</h1>
<p>Name: <%= user.name %></p>
<p>Age: <%= user.age %></p>
  • Now make a server.js file and add the below code to it
JavaScript
const express = require('express');
const app = express();
app.set('view engine', 'ejs');

app.get('/profile', (req, res) => {
  const user = { name: 'Pranshi', age: 30 };
  res.render('profile', { user });  // Renders the 'profile.ejs' template
});

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

Output

Screenshot-2025-03-05-150117
/profile route

5. Asynchronous Request Handling

Since Express is built on top of Node.js, it supports asynchronous request handling. This allows the server to handle multiple requests at once without blocking, improving performance.

JavaScript
const express = require('express');
const app = express();
// Asynchronous route handler
app.get('/data', async (req, res) => {
    try {
      const data = await fetchDataFromDatabase();
      res.json(data);
    } catch (err) {
      res.status(500).send('Error fetching data');
    }
  });
  
  async function fetchDataFromDatabase() {
    // Simulate fetching data (e.g., from a database)
    return { id: 1, name: 'Product A' };
  }
  

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

Output

Screenshot-2025-03-05-150519
/data route


  • Sets up an Express app: It imports express and creates an app object to handle requests.
  • Defines an asynchronous route: The /data route is set up to handle GET requests asynchronously. It tries to fetch data using the fetchDataFromDatabase() function.
  • Fetches data: The fetchDataFromDatabase() function simulates fetching data from a database and returns a product with an id and name.
  • Handles errors and responds: If the data is successfully fetched, it sends the data as a JSON response. If there's an error, it sends a 500 error with the message "Error fetching data".

Conclusion

Express.js offers a range of unique features that make it a powerful yet lightweight framework for building web applications. From its minimalist approach to its powerful routing system, middleware support, templating engines, and asynchronous handling, Express helps developers create scalable and high-performance applications with ease. Whether you are building simple APIs or complex web apps, Express provides the tools you need to get started quickly and efficiently.



Next Article

Similar Reads