-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (33 loc) · 1009 Bytes
/
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
const express = require('express');
const resize = require('./resize');
const fs = require('fs');
const server = express();
server.listen(80, () => {
console.log('Start Images Server');
});
server.get('*', (req, res) => {
const widthString = req.query.w;
const heightString = req.query.h;
const format = req.query.f;
const name = req.params[0];
let width;
let height;
if (widthString) {
width = parseInt(widthString, 10);
}
if (heightString) {
height = parseInt(heightString, 10);
}
fs.exists(`assets/${name}`, exists => {
if (exists) {
res.type(`image/${format || 'jpg'}`);
res.set('Cache-Control', 'public, max-age=86400, no-transform');
resize(`assets/${name}`, format, width, height).pipe(res);
} else {
console.log('Opps!');
res.type(`image/${format || 'png'}`);
res.set('Cache-Control', 'public, max-age=86400, no-transform');
resize(`image-not-found.png`, format || 'png', width, height).pipe(res);
}
});
});