-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
52 lines (40 loc) · 1.14 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
41
42
43
44
45
46
47
48
49
50
51
52
import { handler } from './build/handler.js';
import axios from 'axios';
import express from 'express';
import compression from 'compression';
import sharp from 'sharp';
const app = express();
app.get('/healthcheck', (_req, res) => {
res.send('ok');
});
app.use(compression());
app.use((req, res, next) => {
if (req.path.startsWith('/_app/immutable/') || req.path === '/background.webp') {
res.header('cache-control', 'max-age=31536000');
} else if (req.path.startsWith('/fonts/')) {
res.header('cache-control', 'max-age=31536000');
}
next();
});
app.get('/api/thumbnail/:id', async (req, res) => {
try {
const id = req.params.id;
/**
* @type {import('http').IncomingMessage}
*/
const data = (
await axios.get(`https://i.ytimg.com/vi/${id}/original.jpg`, {
responseType: 'stream'
})
).data;
res.header('content-type', 'image/webp');
res.header('cache-control', 'max-age=1209600');
data.pipe(sharp().resize(640, 360)).pipe(res);
} catch {
res.status(404).send('not found');
}
});
app.use(handler);
app.listen(3000, () => {
console.log('Listening on port 3000');
});