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-morganVerified import paths — ran on the pinned version, not inferred.
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.
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).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.
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.
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.
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.
Upgrade `mongoose-morgan` to version `1.0.12` or higher. This version sets `useNewUrlParser` and `useUnifiedTopology` to `true` by default, resolving these deprecation warnings.
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' }`.