diff --git a/lib/index.js b/lib/index.js index 83267c0..84f9fbe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,13 +7,16 @@ const rateLimit = require('express-rate-limit') const crawl = require('./crawler') const parseHTML = require('./parser') const generateICS = require('./ics') -const { genericErrorHandler, checkURLParameter } = require('./middlewares') +const { genericErrorHandler, checkURLParameter, forceSecure } = require('./middlewares') const port = process.env.PORT const certEndpoint = process.env.CERT_ENDPOINT || '' -const certSecret = process.env.CERT_SECRET +const certSecret = process.env.CERT_SECRET || '' + const app = express() +app.use(forceSecure) + app.set('view engine', 'ejs') app.set('views', path.join(__dirname, 'views')) @@ -21,9 +24,11 @@ app.use(express.static(path.join(__dirname, 'public'))) app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))) app.use(bodyParser()) -app.get(`/${certEndpoint}`, (req, res) => { - res.status(200).send(certSecret) -}) +if (certEndpoint) { + app.get(`/${certEndpoint}`, (req, res) => { + res.status(200).send(certSecret) + }) +} app.get('/', (req, res) => { res.render('index') @@ -55,7 +60,8 @@ app.post('/download', async (req, res, next) => { if (ics) { return res .contentType('text/calendar') - .send(200, new Buffer(ics, 'utf8')) + .status(200) + .send(new Buffer(ics, 'utf8')) } } catch (err) { next(err) diff --git a/lib/middlewares.js b/lib/middlewares.js index 9af02b4..99ef92d 100644 --- a/lib/middlewares.js +++ b/lib/middlewares.js @@ -30,7 +30,15 @@ const checkURLParameter = (req, res, next) => { return next() } +const forceSecure = (req, res, next) => { + if (req.headers['x-forwarded-proto'] === 'http') { + return res.status(301).redirect(`https://${req.headers.host}/`) + } + return next() +} + module.exports = { genericErrorHandler, checkURLParameter, + forceSecure, } diff --git a/package.json b/package.json index a737340..5da7d7c 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "main": "lib/index.js", "scripts": { "start": "node lib/index.js", - "start:dev": "PORT=3000 nodemon lib/index.js", - "start:dev:inspect": "PORT=3000 nodemon --inspect lib/index.js", + "start:dev": "NODE_ENV=development PORT=3000 nodemon lib/index.js", + "start:dev:inspect": "NODE_ENV=development PORT=3000 nodemon --inspect lib/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [