1
0
mirror of https://github.com/comatory/fb2iCal synced 2024-12-27 08:52:58 +01:00
Facebook-Events-iCal-Converter/lib/middlewares.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

const winston = require('winston')
const expressWinston = require('express-winston')
const path = require('path')
const { checkValidURL, createTransports } = require('./utils')
const MissingURLParameter = () => new Error('Please provide valid URL or event number.')
2019-10-17 22:36:29 +02:00
const sendJSON = (req) => {
return req.accepts().includes('application/json')
}
2019-10-17 22:36:29 +02:00
const genericErrorHandler = (err, req, res, next) => {
if (sendJSON(req)) {
res
.status(500)
.send({ error: err.toString() })
return
}
2019-10-17 22:36:29 +02:00
res
.status(500)
.render('error', { error: err.toString() })
}
const checkURLParameter = (req, res, next) => {
2019-10-17 22:36:29 +02:00
const { url } = req.body
if (!url || !checkValidURL(url)) {
return next(MissingURLParameter())
2019-10-17 22:36:29 +02:00
}
return next()
}
2019-10-23 15:15:20 +02:00
const forceSecure = (req, res, next) => {
if (req.headers['x-forwarded-proto'] === 'http') {
return res.status(301).redirect(`https://${req.headers.host}/`)
}
return next()
}
const createRouteLogger = ({ dev }) => {
return expressWinston.logger({
transports: createTransports(),
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
meta: true,
expressFormat: true,
})
}
const createErrorLogger = ({ dev }) => {
return expressWinston.errorLogger({
transports: createTransports(),
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
})
}
2019-10-17 22:36:29 +02:00
module.exports = {
genericErrorHandler,
checkURLParameter,
2019-10-23 15:15:20 +02:00
forceSecure,
createRouteLogger,
createErrorLogger,
2019-10-17 22:36:29 +02:00
}