---
title: "Build an Express middleware to log incoming HTTP requests to the server."  
description: "Build an Express middleware to log incoming HTTP requests to the server."  
author: "Steilla Mitchel"  
published: 2023-07-26  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/159297/build-an-express-middleware-to-log-incoming-http-requests-to-the-server  
category: "javascript"  
tags: ["javascript", "http request", "express js"]  
reading_time: 2 minutes  

---

# Build an Express middleware to log incoming HTTP requests to the server.

Build an [Express](https://www.mindstick.com/forum/156025/how-to-use-garmin-express-software) [middleware](https://www.mindstick.com/forum/159733/what-is-the-role-of-middleware-in-dot-net-core-web-api) to [log](https://www.mindstick.com/articles/126269/the-main-uses-of-log-cabins) incoming [HTTP requests](https://www.mindstick.com/articles/1406/debugging-http-requests-and-http-response) to the server.

## Replies

### Reply by Aryan Kumar

Sure, here is the code for an Express middleware to log incoming [HTTP](https://www.mindstick.com/blog/217/http-endpoints-in-sql-server-2005-2008) requests to the server:

JavaScript

```plaintext
const express = require("express");

const logger = (req, res, next) => {
  const timestamp = new Date().toISOString();
  const method = req.method;
  const url = req.url;
  const ip = req.ip;

  console.log(`${timestamp} ${method} ${url} ${ip}`);
  next();
};

const app = express();

app.use(logger);

app.listen(3000, () => {
  console.log("Server started on port 3000");
});
```

This code first imports the `express` module. Then, it defines a middleware function called `logger`. The `logger` function takes three arguments: the request object, the response object, and the next middleware function.

The `logger` function logs the following information about the request:

- The timestamp of the request
- The HTTP method of the request
- The URL of the request
- The IP address of the client

The `logger` function then calls the `next()` function to pass control to the next middleware function in the stack.

Finally, the app creates a route that listens on the `/` path. This route uses the `logger` middleware to log incoming requests.

To run this code, you can save it as a file called `app.js` and then run it from the command line:

```plaintext
node app.js
```

This will start the Express server on port 3000. You can then make requests to the server and the `logger` middleware will log the requests to the console.


---

Original Source: https://www.mindstick.com/forum/159297/build-an-express-middleware-to-log-incoming-http-requests-to-the-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
