Express.js: A Fast, Minimalist Web Framework for Node.js

Mindula Dilthushan
3 min readMay 26, 2024

--

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. It is widely used for developing server-side applications and APIs. In this article, we’ll explore the key features of Express.js and provide some code examples to illustrate its capabilities.

Key Features of Express.js:

  1. Minimalist Framework: Express.js is designed to be simple and unopinionated, allowing developers to structure their applications as they see fit. This minimalist approach makes it easy to get started and build lightweight applications.
  2. Fast and Efficient: Built on top of Node.js, Express.js leverages the asynchronous, non-blocking nature of Node.js, making it fast and efficient. It can handle a large number of simultaneous connections with high throughput.
  3. Middleware Support: Express.js has a powerful middleware system that allows you to configure and extend the framework easily. Middleware functions can handle tasks such as parsing request bodies, logging, and authentication.
  4. Routing: Express.js provides a straightforward and flexible routing system that allows you to define routes for different HTTP methods and URL patterns. This makes it easy to create RESTful APIs and handle various request types.
  5. Template Engine Support: Express.js supports various template engines, such as Pug, EJS, and Handlebars, which enable you to generate dynamic HTML content easily.

Code Examples in Express.js:

Hello, World!

A simple Express.js application to print “Hello, World!” to the browser.

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

app.get('/', (req, res) => {
res.send('Hello, World!');
});

const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Basic Routing

Defining routes for different HTTP methods.

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

app.get('/', (req, res) => {
res.send('GET request to the homepage');
});

app.post('/', (req, res) => {
res.send('POST request to the homepage');
});

app.put('/user', (req, res) => {
res.send('PUT request to /user');
});

app.delete('/user', (req, res) => {
res.send('DELETE request to /user');
});

const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Using Middleware

Using middleware to log requests.

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

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

app.get('/', (req, res) => {
res.send('Hello, World!');
});

const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Serving Static Files

Serving static files such as images, CSS files, and JavaScript files.

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

// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
res.send('Hello, World!');
});

const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Conclusion:

Express.js is a powerful and flexible web framework that makes it easy to build fast and scalable web applications and APIs. With its minimalist approach, robust routing, and middleware capabilities, Express.js provides a solid foundation for both small and large-scale applications. Whether you are building a simple web server or a complex API, Express.js has the tools and features you need to succeed.

--

--