-
Notifications
You must be signed in to change notification settings - Fork 38
/
server.js
40 lines (37 loc) · 1.09 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const express = require('express');
const url = require('url');
const cors = require('cors');
const request = require('request');
function validUrl(req, res, next) {
if (!req.query.url) {
next(new Error('No url specified'));
} else if (typeof req.query.url !== 'string' || url.parse(req.query.url).host === null) {
next(new Error(`Invalid url specified: ${req.query.url}`));
} else {
next();
}
}
module.exports = () => {
const app = express.Router();
app.get('/', cors(), validUrl, (req, res, next) => {
switch (req.query.responseType) {
case 'blob':
req.pipe(request(req.query.url).on('error', next)).pipe(res);
break;
case 'text':
default:
request({url: req.query.url, encoding: 'binary'}, (error, response, body) => {
if (error) {
return next(error);
}
res.send(
`data:${response.headers['content-type']};base64,${Buffer.from(
body,
'binary'
).toString('base64')}`
);
});
}
});
return app;
};