const express = require('express')
const asyncview = require('asyncview')
const app = express()
app.use(asyncview)
asyncview is a lightweight middleware based on Connect.
It logs every incoming request and the corresponding response as separate events.
If desired, it can also integrate with the debug module.
There are several ways to log HTTP traffic, but each has its trade-offs:
- Logging on request only → You won't see what the server sends back.
- Logging on response only → Two downsides:
- If the response is never sent, the request will remain unlogged.
- Requests and responses are mixed together and lose their timeline clarity.
- Logging both request and response → This is clearer, but it needs a way to link them together.
asyncview solves this by assigning a random ID to each request–response pair.
This makes it a great tool for:
- Debugging projects during development
- Explaining async behavior to students
- Quickly troubleshooting HTTP issues without extra setup
npm install asyncview# Deprecated: so use build instead
git clone https://github.com/shinbatsu/asyncview.git && cd asyncviewnpm installnpm run buildnpm link asyncview
The simplest way to use it:
const app = require('express')()
const asyncview = require('asyncview')
app.use(asyncview) // starts logging
By default, it writes logs to process.stdout.
If you need more control, you can create a custom logger:
const logger = asyncview.custom({ debug: true }) // uses namespace 'http'
// const logger = asyncview.custom({ debug: 'custom-namespace' })
// const logger = asyncview.custom({ debug: debugInstance })
app.use(logger)
The debug option works with the debug module:
| value | behavior |
|---|---|
true |
creates a debug instance with the default namespace 'http' |
string |
creates a debug instance using the given namespace |
function |
uses any custom function (for example, an existing debug instance). It will be called with colorized output |
If you need more advanced or flexible logging, you might prefer:
