nodejs logo

What is Express.js?

Overview

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 simplifies the process of handling HTTP requests and responses, allowing developers to create APIs and web applications quickly and efficiently.

Key Features

  • Middleware support for handling requests and responses.
  • Routing capabilities for defining endpoints.
  • Support for template engines to render dynamic content.
  • Easy integration with various databases.
  • Robust error handling and logging features.

Installing Express

You can easily install Express using npm. Run the following command:

npm install express

Creating a Simple Express Server

Here's how to set up a basic Express server:

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

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

app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

This code creates a simple Express server that responds with "Hello, Express!" when you access the root URL.

Defining Routes

Express makes it easy to define routes for your application:

app.get('/about', (req, res) => {
  res.send('About Page');
});

app.get('/contact', (req, res) => {
  res.send('Contact Page');
});

The above code defines additional routes for "/about" and "/contact", each returning a simple response.

© 2024 Express.js Guide