From 69406c43ea899c3956aa16a9e0834aff40c8510f Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sat, 27 May 2017 11:44:07 +0200 Subject: Rewrite /meetingpointProxy into /get This makes it simple and more secure, and this improves server logging. --- routes/getSchedule.js | 36 ++++++++++++++++++++++++++++++++++++ routes/meetingpointProxy.js | 19 ------------------- 2 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 routes/getSchedule.js delete mode 100644 routes/meetingpointProxy.js (limited to 'routes') diff --git a/routes/getSchedule.js b/routes/getSchedule.js new file mode 100644 index 0000000..ed9f1a9 --- /dev/null +++ b/routes/getSchedule.js @@ -0,0 +1,36 @@ +const express = require('express') +const router = express.Router() +const request = require('request') +const iconv = require('iconv-lite') + +const getUserIndex = require('../lib/getUserIndex') +const getURLOfUser = require('../lib/getURLOfUser') + +router.get('/:type/:value', function (req, res, next) { + getUserIndex().then(users => { + const { type, value } = req.params + const { week } = req.query + const user = + users.filter(user => user.type === type && user.value === value)[0] + + if (!user) { + next(new Error(`${type}${value} is not in the user index.`)) + } + + const { index } = user + + const url = getURLOfUser(type, index, week) + + request(url, { encoding: null }, function (err, data) { + if (err) { + next(err) + return + } + + const utf8Body = iconv.decode(data.body, 'ISO-8859-1') + res.status(data.statusCode).end(utf8Body) + }) + }) +}) + +module.exports = router diff --git a/routes/meetingpointProxy.js b/routes/meetingpointProxy.js deleted file mode 100644 index 68f3fdf..0000000 --- a/routes/meetingpointProxy.js +++ /dev/null @@ -1,19 +0,0 @@ -const express = require('express') -const router = express.Router() -const request = require('request') -const iconv = require('iconv-lite') - -router.get('/:url', function (req, res, next) { - const url = `http://www.meetingpointmco.nl/${req.params.url}` - request(url, { encoding: null }, function (err, data) { - if (err) { - next(err) - return - } - - const utf8Body = iconv.decode(data.body, 'ISO-8859-1') - res.status(data.statusCode).end(utf8Body) - }) -}) - -module.exports = router -- cgit v1.1