How to use the app object in Express ?
Last Updated :
28 Apr, 2025
In Node and Express, the app
object has an important role as it is used to define routes and middleware, and handling HTTP requests and responses. In this article, we'll explore the various aspects of the `app` object and how it can be effectively used in Express applications.
Prerequisites
The app
object represents the Express application. It can be created by calling the express()
function and is used to set up and configure various aspects of your server.
Syntax:
const express = require('express');
const app = express();
Different functions of the app object:
1. Defining Routes with "app"
The `app` object is to define routes for handling different HTTP methods and paths. Routes are defined using methods like GET, POST, PUT and DELETE.
Here's a simple example:
// Handling GET requests to the root path "/"
app.get('/', (req, res) => {
res.send('Hello, World!');
});
2. Middleware with "app"
Middleware functions are functions that have access to the request (`req`), response (`res`), and the next middleware function in the application’s request-response cycle. The `app` object is used to apply middleware to your application.
Syntax:
// Logger middleware
const loggerMiddleware = (req, res, next) => {
console.log(`Received a ${req.method} request to ${req.path}`);
next();
};
// Applying the logger middleware to all routes
app.use(loggerMiddleware);
3. Setting Configuration with "app"
The `app` object allows you to configure various settings for your application, such as the view engine, port number, and more.
Syntax:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Steps to create a sample Express app.
Step 1: Create a directory for the application
mkdir app-object
cd app-object
Step 2: Initializing the app and install the required dependencies.
npm init -y
npm i express
Example: In this sample code we have used various app objects.
JavaScript
const express = require("express");
const app = express();
// Handling GET requests to the root path "/"
app.get("/", (req, res) => {
res.send("Hello, World!");
});
// Handling POST requests to the "/login" path
app.post("/login", (req, res) => {
res.send("Login successful!");
});
// Logger middleware
const loggerMiddleware = (req, res, next) => {
console.log(`Received a ${req.method} request to ${req.path}`);
next();
};
// Applying the logger middleware to all routes
app.use(loggerMiddleware);
// Handling GET requests to the "/about" path
app.get("/about", (req, res) => {
res.send("About Us");
});
// Setting the port number
const PORT = process.env.PORT || 3001;
// Starting the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Output(Browser):
output when the "localhost:3000/" endpoint is hitted. Output(Console):
console when server is connected and when the middleware is called
Similar Reads
How to use express in typescript ? In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nod
2 min read
How to use get parameter in Express.js ? Express Js is a web application framework on top of Node.js web server functionality that reduces the complexity of creating a web server. Express provides routing services i.e., how an application endpoint responds based on the requested route and the HTTP request method (GET, POST, PUT, DELETE, UP
2 min read
How to use Template Engines in Express JS ? Express.js is a popular web framework for Node.js that simplifies the process of building web applications. One of the key features of Express is its ability to integrate with template engines, allowing developers to dynamically generate HTML pages with data from their server. In this article, we'll
3 min read
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
How to use third-party middleware in Express JS? Express JS is a robust web application framework of Node JS which has the capabilities to build web and mobile applications. Middleware is the integral part of the framework. By using the third party middleware we can add additional features in our application.PrerequisitesNode JSExpress JSPostmanTa
2 min read