monitor-request
A simple and flexible API request logging middleware for Node.js. This package allows you to log HTTP requests in an Express app and store them in a log file.
Table of Contents
Installation
To install the package, use npm:
npm install monitor-request
Usage
You can use the monitor
middleware to log incoming HTTP requests. Import the middleware in your Express app:
const monitor = require("monitor-request");
const express = require("express");
const app = express();
// Use the monitor middleware
app.use(monitor());
Options
The monitor
middleware does not require any parameters, but it logs essential request details, including:
- Client IP address
- HTTP method
- Request URL
- Response status code
- Content length
- Referrer and User-Agent headers
- Response time
The logs are saved to a file named monitor.log
in your project directory.
Examples
Basic Usage
const express = require("express");
const monitor = require("monitor-request");
const app = express();
// Use the monitor middleware to log requests
app.use(monitor());
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
Custom Log Example
A sample log entry in monitor.log
might look like this:
::1 - - [20/Oct/2024:07:20:04 +0000] "GET /api/v1/user/find HTTP/1.1" 200 117 "-" "PostmanRuntime/7.42.0" - 327.472 ms
This log contains the IP address, request method, URL, HTTP version, status code, content length, referrer, user-agent, and response time.
License
This project is licensed under the MIT License.