Registry / database / mongoose-morgan

mongoose-morgan

JSON →
library1.0.17jsnpmunverified

mongoose-morgan is an npm package designed to integrate Express.js HTTP request logging (via morgan) directly into a MongoDB database (via mongoose). The current stable version is 1.0.17, which primarily focuses on updating dependencies to maintain compatibility and security. Historically, the package has seen irregular but focused feature additions, such as support for capped collections and improved handling of MongoDB Atlas connection strings. Its key differentiator is simplifying the process of persisting detailed HTTP access logs into a NoSQL database, offering configurable options for the target collection, user authentication, and advanced MongoDB connection parameters without requiring separate setup for both logging and database interaction. It's a pragmatic choice for Express applications already utilizing Mongoose for data persistence that need a straightforward way to store request logs.

npm install mongoose-morgan
INSTALL
IMPORT
SIG · MONGOOSE-MORGAN
M
mongoose-morgan
databasejavascriptv1.0.17
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

morgan
import morgan from 'mongoose-morgan';
const morgan = require('mongoose-morgan');
While the package's examples use CommonJS `require`, ESM `import` is the modern approach. The default export is the middleware function.
mongooseMorgan
import * as mongooseMorgan from 'mongoose-morgan';
import { morgan } from 'mongoose-morgan';
If your linter or TypeScript configuration has issues with the default import, importing as a namespace object is an alternative, though less common for single-function modules.
MorganMiddleware
import type { MorganMiddleware } from 'mongoose-morgan';
Although types were reverted to `any` in v1.0.15, for potential future-proofing or if external type definitions are used, this is the pattern for type imports.

This example sets up an Express application to log all incoming HTTP requests to a MongoDB database using `mongoose-morgan`. It demonstrates basic usage with a connection string and two example routes to generate logs.

import express from 'express'; import morgan from 'mongoose-morgan'; import mongoose from 'mongoose'; const app = express(); const port = process.env.PORT || 8080; // Ensure MongoDB connection for mongoose-morgan // In a real app, you'd typically connect Mongoose once at app startup. // For simplicity here, mongoose-morgan handles its own connection based on connectionString. app.use(morgan({ connectionString: process.env.MONGO_URI || 'mongodb://localhost:27017/logs-db' })); app.get('/', (req, res) => { res.send('Hello World! Check your MongoDB logs.'); }); app.get('/error', (req, res) => { res.status(500).send('An error occurred.'); }); // Close Mongoose connection on app shutdown (optional, but good practice) process.on('SIGINT', async () => { await mongoose.disconnect(); console.log('MongoDB connection disconnected through app termination'); process.exit(0); }); app.listen(port, () => { console.log(`Server running on port ${port}. Visit http://localhost:${port}`); });
Debug
Known issues
gotchaOlder versions of `mongoose-morgan` (prior to `v1.0.12`) might emit deprecation warnings from Mongoose regarding `useNewUrlParser` and `useUnifiedTopology`.
fix
Upgrade to `mongoose-morgan@1.0.12` or higher, which defaults these options to `true` to suppress the warnings. If you cannot upgrade, manually pass `{ useNewUrlParser: true, useUnifiedTopology: true }` in the `connectionOptions` parameter if it were exposed (it's not directly exposed in the main function signature, but rather handled internally).
affects: <1.0.12
gotchaThe TypeScript types for `mongoose-morgan` were explicitly changed from `object` to `any` in `v1.0.15` due to an issue, significantly reducing type safety for TypeScript users.
fix
Be aware that type checking will be minimal for `mongoose-morgan`'s configuration. Consider creating your own declaration file (`.d.ts`) or casting the configuration object if strong typing is critical.
affects: >=1.0.15
gotchaWhen connecting to MongoDB Atlas using the `mongodb+srv` URI scheme, specifying the database name directly in the connection string might not work as expected. Before `v1.0.7`, `dbName` had to be handled carefully.
fix
Upgrade to `mongoose-morgan@1.0.7` or newer, which introduced a `dbName` option in the `mongoData` configuration to explicitly specify the database name for `mongodb+srv` connections.
affects: <1.0.7
gotchaVersions `1.0.7` and some very early `1.0.x` releases had a bug where the `morgan.token` function was missing or improperly handled, preventing custom token definitions.
fix
Ensure you are using `mongoose-morgan` versions `1.0.6` or `1.0.8` or newer, as this issue was fixed and then re-fixed.
affects: 1.0.7, <1.0.6
Errors
Common errors & fixes
TypeError: morgan.token is not a function
Using `mongoose-morgan` version `1.0.7` or a very early `1.0.x` version (prior to `1.0.6`) which had a bug related to `morgan.token` availability.
fix
Upgrade `mongoose-morgan` to version `1.0.8` or a more recent version (e.g., `1.0.17`) to get the fix for the `morgan.token` functionality.
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
This warning originates from Mongoose, indicating an older parser is being used. `mongoose-morgan` versions prior to `1.0.12` did not explicitly set `useNewUrlParser` or `useUnifiedTopology`.
fix
Upgrade `mongoose-morgan` to version `1.0.12` or higher. This version sets `useNewUrlParser` and `useUnifiedTopology` to `true` by default, resolving these deprecation warnings.
MongooseError: The `dbName` option is required when using `mongodb+srv` connections.
Attempting to connect to MongoDB Atlas (or another `mongodb+srv` URI) with `mongoose-morgan` versions older than `1.0.7` without correctly specifying the database name in the connection string or via the `dbName` option.
fix
Upgrade `mongoose-morgan` to version `1.0.7` or newer and use the `dbName` property within the `mongoData` configuration object: `{ connectionString: 'mongodb+srv://user:pass@cluster...', dbName: 'yourDatabaseName' }`.
Upgrade
Version history
1.0.17latest on npm
Audit
Dependencies
morganrequiredProvides the HTTP request logging middleware.
mongooserequiredHandles MongoDB connection and schema management.
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
2
Resources
mongoose-morgan — npm install mongoose-morgan · libregistry