DEV Community

Nemanja Petrovic
Nemanja Petrovic

Posted on • Edited on

My first npm package mongoose-morgan

Hi,

Recently I was working on one of my express app projects, and I had a need to log all data from morgan npm package to the database. So I found out there are some npm packages available to do it, but I had to create one of my own.

The package is named mongoose-morgan, here is a link to it.

So mongoose-morgan is an npm package express middleware which is combining mongoose and morgan packages by adding an additional functionality to log morgan data into MongoDB.

To install it just call:

npm install mongoose-morgan
Enter fullscreen mode Exit fullscreen mode

And to use it with all of its features just add this code into your express app.

app.use(morgan({
    collection: 'error_logger'
    connectionString: 'mongodb://localhost:27017/logs-db',
    user: 'admin',
    pass: 'test12345'
   },
   {
    skip: function (req, res) {
        return res.statusCode < 400
    }
   },
   'dev'
));
Enter fullscreen mode Exit fullscreen mode

The more important is that you have all available features from morgan package like format and options features, so you can select whatever you want.

Top comments (4)

Collapse
 
davedodea profile image
Dave O'Dea • Edited

Awesome job, @nempet ! This is very useful - super quick way to store application data to MongoDB :)

Collapse
 
nempet profile image
Nemanja Petrovic

Thank you so much! I hope it saves you our most valuable resource - time!

Collapse
 
danieljsummers profile image
Daniel J. Summers

I recently published my first package to npm as well - it's a rush, isn't it? :) Congratulations!

Collapse
 
nempet profile image
Nemanja Petrovic

Thanks, congratulations to you too.

Yeah, it's a great feeling knowing someone is using your npm package :D