Skip to content

Commit

Permalink
added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Matić committed Oct 26, 2023
1 parent 1b4efb0 commit e504127
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 2 deletions.
21 changes: 21 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@ const app = express()
const fs = require('node:fs')
const https = require('node:https')
const http = require('node:http')
const winston = require('winston')
const httpsport = 3443
const httpport = 3080

const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(info => `[${info.timestamp}] ${info.message}`)
),
transports: [
new winston.transports.Console(),
// new winston.transports.File({ filename: 'app.log' })
]
});


app.set('view engine', 'ejs')

app.use(express.static(__dirname + '/public'));

// redirect middleware
app.use((req, res, next) => {
if(req.protocol === 'http') {
logger.info(`Redirected ${req.ip} to HTTPS: ${req.method} ${req.url}`)
return res.redirect(`https://${req.headers.host}${req.url}`);
}
next();
})

// logging middleware
app.use((req, res, next) => {
logger.info(`Request from IP ${req.ip}, User Agent: ${req.userAgent}, Method: ${req.method}, URL: ${req.url}`);

next();
})

app.get('/', (req, res) => {
res.render('index.ejs')
})
Expand Down
228 changes: 227 additions & 1 deletion app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e504127

Please sign in to comment.