---
title: "How do you define a route in Express?"  
description: "How do you define a route in Express?"  
author: "Ravi Vishwakarma"  
published: 2025-04-03  
updated: 2025-04-03  
canonical: https://www.mindstick.com/interview/34025/how-do-you-define-a-route-in-express  
category: "javascript"  
tags: ["javascript", "express js"]  
reading_time: 2 minutes  

---

# How do you define a route in Express?

In Express (a Node.js web framework), you define a route using methods like `app.get()`, `app.post()`, `app.put()`, etc., depending on the HTTP verb you want to handle.

## Here’s the basic syntax:

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

// Define a route
app.get('/example', (req, res) => {
  res.send('This is the /example route!');
});

// Start the server
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
```

#### Breakdown:

`app.get()` defines a GET route.

The first argument (`'/example'`) is the route path.

The second argument is a callback function (called a *route handler*) with `req` (request) and `res` (response) parameters.

`res.send()` sends a response to the client.

#### Other common HTTP methods:

```javascript
app.post('/submit', (req, res) => { ... });
app.put('/update/:id', (req, res) => { ... });
app.delete('/delete/:id', (req, res) => { ... });
```

## Answers

### Answer by Ravi Vishwakarma

In Express (a Node.js web framework), you define a route using methods like `app.get()`, `app.post()`, `app.put()`, etc., depending on the HTTP verb you want to handle.

## Here’s the basic syntax:

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

// Define a route
app.get('/example', (req, res) => {
  res.send('This is the /example route!');
});

// Start the server
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
```

#### Breakdown:

`app.get()` defines a GET route.

The first argument (`'/example'`) is the route path.

The second argument is a callback function (called a *route handler*) with `req` (request) and `res` (response) parameters.

`res.send()` sends a response to the client.

#### Other common HTTP methods:

```javascript
app.post('/submit', (req, res) => { ... });
app.put('/update/:id', (req, res) => { ... });
app.delete('/delete/:id', (req, res) => { ... });
```


---

Original Source: https://www.mindstick.com/interview/34025/how-do-you-define-a-route-in-express

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
