-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (39 loc) · 1.02 KB
/
index.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
import express from "express";
import { createSrcSet } from "img-optimizer";
import { createOptimizer } from "img-optimizer/server";
const app = express();
app.get("/", (req, res) => {
res.send(`
<html>
<style>
body{
margin: 0;
}
</style>
<head>
<title>Img Optimizer</title>
</head>
<body>
<img srcset="${createSrcSet("/test-8k.jpg")}" />
<img srcset="${createSrcSet(
"https://pbs.twimg.com/media/ELSHvYBUUAAH4j1.jpg:large"
)}" />
</body>
</html>
`);
});
const optimize = createOptimizer({
domains: ["pbs.twimg.com"],
});
app.use(express.static("public"));
app.get("/img-optimizer", async (req, res, next) => {
const result = await optimize({
url: req.url,
headers: req.headers,
});
const { body, status, headers } = result;
res.status(status).header(headers).send(body);
});
app.listen(3000, () => {
console.log("Listening on http://localhost:3000");
});