---
id: 69f3046f4c0e76cbdd24858f
title: How Does Debugging and Logging Work in Express?
challengeType: 19
dashedName: how-does-debugging-and-logging-work-in-express
---

# --description--

Have you ever had your Express app break with no clear error message? Debugging and logging can help you see exactly what's happening behind the scenes, so you can find and fix issues faster.

In this lesson, we'll explore how Express handles debugging and logging—and how you can use it to improve your development workflow.

## Built-In Debugging with Express

Express already includes a debugging tool called `debug`. You don't need to install anything to use it.

To turn on debugging, all you need to do is set the `DEBUG` environment variable when you run your app.

Here's an example:

```js
DEBUG=express:* node index.js
```

This command enables all debugging messages under the `express:*` namespace. You'll start seeing output like:

```js
express:router:route dispatching GET /
express:router:layer processing GET /
```

These messages help you trace route dispatches, middleware calls, and more. It's a great way to understand the flow of requests in your app.

Want to be more specific? You can narrow down the namespace:

```js
DEBUG=express:router node index.js
```

This will only show logs related to Express's router logic.

On Windows, the syntax is slightly different for PowerShell:

```js
$env:DEBUG = "express:*"; node index.js
```

And for CMD, it looks like this:

```js
set DEBUG=express:* && node index.js
```

Same idea, just a different way to set environment variables.

Now let's look at logging in Express.

For logging HTTP requests, many developers use middleware like `morgan`.

Here's how to use it:

```bash
npm install morgan
```

Then add it to your app:

```js
const express = require('express')
const morgan = require('morgan')
const app = express()

app.use(morgan('dev'))s
```

Now, every incoming request will be logged to the terminal with details like the method, URL, status code, and response time.

And why does this matter?

Debugging gives you insight into how Express is handling requests, middleware, and routing without cluttering your production logs.

Logging gives you a record of what's happening across requests, which is crucial for debugging bugs, understanding traffic, and spotting issues early.

Together, they give you control and clarity in your development process.

# --questions--

## --text--

How do you enable debugging messages in Express?

## --answers--

By using `console.log()`.

### --feedback--

Think about environment configuration.

---

By installing a debug plugin

### --feedback--

Think about environment configuration.

---

By setting the `DEBUG` environment variable

---

By passing `{ debug: true }` to the Express constructor

### --feedback--

Think about environment configuration.

## --video-solution--

3

## --text--

What does `DEBUG=express:router` do?

## --answers--

Enables debugging for all routers.

### --feedback--

Focus on namespace filtering.

---

Logs all route names.

### --feedback--

Focus on namespace filtering.

---

Enables only router-related debug output.

---

Disables debugging entirely

### --feedback--

Focus on namespace filtering.

## --video-solution--

3

## --text--

What is a common library for logging HTTP requests in Express?

## --answers--

`axios`

### --feedback--

It logs method, URL, status, and response time.

---

`morgan`

---

`lodash`

### --feedback--

It logs method, URL, status, and response time.

---

React

### --feedback--

It logs method, URL, status, and response time.


## --video-solution--

2
