-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (55 loc) · 1.71 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
const rateLimit = require("express-rate-limit");
const PImage = require("pureimage");
const fs = require("fs");
const path = require("path");
const pixel_profile_pic = require("./pixel-profile-pic");
require("dotenv").config();
const port = process.env.PORT;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get("/api", (req, res) => {
const width = parseInt(req.query.width) || 20;
const cell = parseInt(req.query.cell) || 7;
const color = "#" + (req.query.color || "7fffd4");
const seed = req.query.seed || "default";
const img = pixel_profile_pic(width, cell, color, seed);
const timestamp = Math.floor(Date.now() / 1000);
//write to 'out.png'
PImage.encodePNGToStream(
img,
fs.createWriteStream(path.join(__dirname, "images", timestamp + ".png"))
)
.then(() => {
res.sendFile(
timestamp + ".png",
{
root: path.join(__dirname, "images"),
},
(err) => {
fs.unlinkSync(path.join(__dirname, "images", timestamp + ".png"));
}
);
})
.catch((e) => {
console.log(e);
res.send("Oops, something went wrong 💩");
});
});
app.use("*", (req, res, next) => {
res.send("Not found ⛔");
});
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: process.env.NODE_ENV === "production" ? 100 : false,
standardHeaders: true,
legacyHeaders: false,
handler: (req, res, next) => {
const error = new Error("Too many requests");
error.status = StatusCodes.TOO_MANY_REQUESTS;
next(error);
},
});
app.use(limiter); // limits all paths
app.listen(port, () => console.log(`Server started on port ${port}`));