[[:oktatas:web:back-end_framework:express|< Express]]
====== Express - Naplózás ======
* **Szerző:** Sallai András
* Copyright (c) 2023, Sallai András
* Szerkesztve: 2024
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== Morgan =====
Webhely:
* https://www.npmjs.com/package/morgan (2023)
pnpm install morgan
const Router = require('express')
const router = new Router()
router.get('/msg', (req, res) => {
res.json({msg: 'Működik'})
})
module.exports = router
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ó:
* combined
* dev
* short
* tiny
===== Írás fájlba =====
const Router = require('express')
const router = new Router()
router.get('/msg', (req, res) => {
res.json({msg: 'Működik'})
})
module.exports = router
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')