Express app.get() Request Function (original) (raw)

Last Updated : 3 Jun, 2026

The app.get() function in Express is used to define routes that handle GET requests. It lets an application respond when a user visits a specific URL, making it one of the most common ways to send web pages, fetch data, or display content in an Express app.

**Syntax

app.get( path, callback )

where,

Working of app.get() in Express

Below are the basic steps to understand how app.get() works.

1. Install Express

First, install Express in your project using npm:

npm install express

2. 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();

`

3. 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!'); });

`

4. 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'); });

`

**Example

JavaScript ``

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

// Define a GET route app.get('/', (req, res) => { res.send('

Welcome to Express.js!

'); });

app.listen(PORT, () => { console.log(Server listening on PORT ${PORT}); });

``

**Output

Simple use of Express.get()

Example using app.get()

This code imports Express, creates an Express application, defines a GET route for the root URL (/) that returns an HTML response, and starts a server listening on port 3000.

**Example 2: Express allows you to add middleware to routes, which are functions that execute before the final response.

JavaScript ``

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

// Define a GET route app.get('/', (req, res) => { res.send('

Welcome to Express.js!

'); });

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('

Response after middleware

'); });

``

Screenshot-2025-03-04-130848

Example of middleware with app.get()

This code imports and initializes an Express app, defines a basic route and a middleware route, executes middleware using next(), and sends HTML responses to complete the request-response cycle.

Use cases of app.get()

Here are some practical use cases of app.get():

**Advantages of using app.get()