1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-06-05 22:09:25 +02:00

move app routes to separate directory for better composition in

preparation of firebase integration
This commit is contained in:
Ondřej Synáček
2020-11-01 13:08:48 +01:00
parent c7175382b6
commit 0ad6bba142
4 changed files with 78 additions and 45 deletions

42
lib/routes/download.js Normal file
View File

@ -0,0 +1,42 @@
const { getNormalizedUrl } = require('../utils')
const crawl = require('../services/crawler')
const { retrieveICS } = require('../services/ics-retriever')
const downloadHTML = (logger) => (async (req, res, next) => {
try {
const { url } = req.body
const facebookURL = getNormalizedUrl(url)
const html = await crawl(facebookURL, { logger })
res
.contentType('text/html')
.status(200)
.send(Buffer.from(html, 'utf8'))
} catch (err) {
next(err)
}
})
const download = async (req, res, next) => {
try {
const { url } = req.body
const ics = await retrieveICS(url, {
logger: appLogger,
crawl,
})
res
.contentType('text/calendar')
.status(200)
.send(Buffer.from(ics, 'utf8'))
} catch (err) {
next(err)
}
}
module.exports = {
downloadHTML,
download,
}

16
lib/routes/error.js Normal file
View File

@ -0,0 +1,16 @@
const error = (req, res) => {
const error = req.error || req.query.error || ''
res
.status(500)
.render('error', { error })
}
const notFound = (req, res) => {
res.status(400).render('404')
}
module.exports = {
error,
notFound,
}

9
lib/routes/index.js Normal file
View File

@ -0,0 +1,9 @@
const { error, notFound } = require('./error')
const { download, downloadHTML } = require('./download')
module.exports = {
error,
notFound,
download,
downloadHTML,
}