Webhely:
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ó:
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')