Express app.get() Request Function
Last Updated :
15 Jul, 2025
The app.get() function is used to define routes on your server that handle HTTP GET requests. A GET request is typically used when the client asks the server to send back some information, like retrieving a webpage or data from a database.
It’s an essential part of building websites and APIs, as it allows developers to specify what happens when a client requests data, whether it's showing a webpage, fetching information, or pulling data from a database.
Syntax
app.get( path, callback )
In this syntax:
- path: It defines the route for which the middleware function is triggered.
- callback: It can be one or more functions that process the request that has been made to the server.
How does app.get() work in Express?
Below are the basic steps to understand how app.get() works.
1. Import Express and Create an App
This code imports the Express module and creates an Express application instance. This app instance is used to define routes and handle HTTP requests.
JavaScript
const express = require('express');
const app = express();
2. Define a GET Route
This code defines a route that listens for GET requests at /hello. When accessed, it responds with "Hello, World!".
JavaScript
app.get('/hello', (req, res) => {
res.send('Hello, World!');
});
3. Start the Server
This code starts the server and makes it listen on port 3000. When the server runs, it logs "Server is running on port 3000" to the console.
JavaScript
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Steps to create the application
To use Express in your project, follow these steps:
Step 1: Install ExpressJs
To begin, you need to install Express. You can do this by running the following command in your terminal:
npm install express
Step 2: Verify Installation
Once Express is installed, you can check the version to make sure it’s set up correctly. Run this command in your terminal to confirm the installation:
npm version express
Project Structure

The package.json will show Express in the dependencies:
"dependencies": {
"express": "^5.1.0",
}
Example using app.get()
Here is a simple example to demonstrate app.get() in action:
JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
// Define a GET route
app.get('/', (req, res) => {
res.send('<h1>Welcome to Express.js!</h1>');
});
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
Output:
Example using app.get() In this example:
- Importing Express: It imports the Express.js framework using require('express'), allowing us to create a web server.
- Creating an Express App: The app variable is initialized with express(), which creates an instance of an Express application.
- Defining a Route: A GET request handler is defined for the root ('/') route, responding with an HTML message: "Welcome to Express.js!".
- Starting the Server: The server listens on port 3000, and when started, logs a message indicating it's running.
Using Middleware with app.get()
Express allows you to add middleware to routes, which are functions that execute before the final response. Here’s an example of using middleware with app.get():
JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
// Define a GET route
app.get('/', (req, res) => {
res.send('<h1>Welcome to Express.js!</h1>');
});
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
app.get('/middleware', (req, res, next) => {
console.log('Middleware executed');
next();
}, (req, res) => {
res.send('<h1>Response after middleware</h1>');
});
Example of middleware with app.get()In this example:
- Importing and Initializing Express: The code imports Express.js and creates an instance of the Express application (app). The server is set to listen on port 3000.
- Defining a Basic Route: A GET request handler for '/' sends a simple HTML response: "<h1>Welcome to Express.js!</h1>".
- Using Middleware in a Route: The '/middleware' route includes a middleware function that logs "Middleware executed" to the console before passing control to the next function using next().
- Handling the Final Response: After the middleware function runs, the next function sends the response: "<h1>Response after middleware</h1>", completing the request-response cycle.
Use cases of app.get()
Here are some practical use cases of app.get():
- Serving Web Pages: app.get() can be used to deliver static or dynamic web pages when users visit specific URLs.
- Retrieving Data: It helps fetch and send data from the server to clients, such as user details, product information, or articles.
- Handling URL Parameters: The function can process dynamic URL parameters to customize responses based on user input.
- Processing Query Strings: It allows handling query parameters in the request URL to filter or customize the data sent back to the client.
- API Endpoint Creation: app.get() is commonly used to define API routes that return JSON data, making it essential for building RESTful web services.
Advantages of using app.get()
Using app.get() offers several benefits for handling HTTP GET requests:
- It simplifies defining routes for retrieving data from the server.
- The code remains clean and easy to read, making route handling straightforward.
- Middleware can be added before the response, enabling tasks like logging, authentication, or data processing.
- It keeps the application modular, making it easier to manage as it grows.
When building an Express app, optimizing performance ensures that the application runs smoothly and quickly. Here are a few simple techniques to enhance performance when using app.get()
- Caching Responses: Caching involves saving a copy of frequently requested data so it doesn't need to be retrieved every time a request is made. This helps improve the app's speed by reducing repetitive data fetching.
- Compression: Compression makes the data smaller, which helps it travel faster from the server to the client, especially when dealing with large files. By turning on compression, less data needs to be sent, making the transfer quicker.
- Asynchronous Code: Asynchronous code helps the server stay active while it waits for tasks like database queries or API calls to finish. This way, the server can keep working on other requests without getting stuck waiting for one task to complete
Conclusion
The get() function is used in many ways, with or without middleware. It helps you set up routes, send dynamic content, and create APIs. You can use it to handle requests, like when you need to show a webpage or get data. Mastering this function makes it easier to get data, improve app speed, and keep your web applications organized.
Similar Reads
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.Basics of Web Development To better understand t
7 min read
HTML Tutorial
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
JS Tutorial
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.Client Side: On the client side, JavaScript works
11 min read
JSON TutorialJSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. Used Extensively : Used in APIs, configuration files, and data exchange between servers and clients.Text-based: JSON is a simple text format, making it lightweight and easy to transmit.Human
5 min read
TypeScript TutorialTypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications.Static typing allows you to
8 min read
Vue.js TutorialVue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
jQuery TutorialjQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
Front End
React TutorialReact is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
Angular TutorialAngular is a powerful, open-source web application framework for building dynamic and scalable single-page applications (SPAs). Developed by Google, Angular provides a comprehensive solution for front-end development with tools for routing, form handling, HTTP services, and more.Designed for buildin
4 min read
Backend
Node.js TutorialNode.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
4 min read
Express.js TutorialExpress.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities.Built on Node.
4 min read
PHP TutorialPHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana
9 min read
Laravel TutorialLaravel is an open-source PHP web application framework that has gained immense popularity since its inception in 2011, created by Taylor Otwell. This renowned framework empowers developers to build robust, scalable web applications with remarkable ease. As a developer-friendly framework, Laravel of
3 min read
Database
Web Technologies Questions The following Web Technologies Questions section contains a wide collection of web-based questions. These questions are categorized based on the topics HTML, CSS, JavaScript, and many more. Each section contains a bulk of questions with multiple solutions. Table of Content HTML QuestionsCSS Question
15+ min read