2

I do not understand the stream in node well. I try to save my app's request and response information to the mongodb.

Firstly, I want to set a variable to the result of morgan:

var apiInfo = morgan('dev')
   app.use(console.log('test______',apiInfo))

However, it does not work. I know this in the morgan official site:

var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var path = require('path')

var app = express()

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'})

// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))

app.get('/', function (req, res) {
  res.send('hello, world!')
})

But I do not want to save the result to the access.log, and I just want to save the result to a variable apiInfo.

Cœur
  • 37,241
  • 25
  • 195
  • 267
leon
  • 49
  • 6

1 Answers1

1

use the project https://www.npmjs.com/package/mongo-morgan. Use regular expressions to modify the stream in the index.js , and then you can easily put Morgan's data to Mongodb

leon
  • 49
  • 6
two month
  • 26
  • 2