-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
36 lines (30 loc) · 1.07 KB
/
app.js
File metadata and controls
36 lines (30 loc) · 1.07 KB
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
const express = require('express');
const axios = require('axios');
const app = new express();
const img = require('fs').readFileSync('./public/images/404.jpg');
app.listen(process.env.PORT || 3000);
app.use(express.static(process.cwd() + '/public'))
app.get('/', (req, res) => res.sendFile('./public/index.html'));
const cors = require('cors')
app.use(cors())
app.get('*', (req, res) => {
let url = req.originalUrl.replace('/', '');
axios.get(url, {
responseType: 'arraybuffer'
})
.then(response => {
if (response.headers['content-type'].split('/')[0] == 'image') {
res.set('Content-Type', response.headers['content-type']);
if(typeof response.data === 'object') res.send(response.data);
res.send(Buffer.from(response.data, 'binary'));
}
else {
throw new Error('Not an image');
}
})
.catch(err => {
// console.log(err.message);
res.set('Content-Type', 'image/jpg');
res.send(img);
})
});