Tartalomjegyzék

< Express

Express - Naplózás

Morgan

Webhely:

pnpm install morgan
src/api.js
const Router = require('express')
const router = new Router()
 
router.get('/msg', (req, res) => {
    res.json({msg: 'Működik'})
})
 
module.exports = router
src/index.js
const express = require('express')
const app = express()
const morgan = require('morgan')
const router = require('./api')
 
app.use(morgan('dev'))
 
app.use('/api', router)
 
app.listen(8000, () => {
    console.log('Listening on port: 8000')
})

Paraméterként megadható:

Írás fájlba

app/api.js
const Router = require('express')
const router = new Router()
 
router.get('/msg', (req, res) => {
    res.json({msg: 'Működik'})
})
 
module.exports = router
app/index.js
const express = require('express')
const morgan = require('morgan')
const app = express()
const fs = require('fs')
const path = require('path')
const router = require('./api')
 
const logfile = 'access.log'
var accessLogStream = fs.createWriteStream(logfile, { flags: 'a' })
app.use(morgan('dev', { stream: accessLogStream }))
 
app.use('/api', router);
 
app.listen(8000, () => {
    console.log('Listening on port: 8000')
})

Ha az index.js fájl mellé szeretnénk:

var logfile = path.join(__dirname, 'access.log')