-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (56 loc) · 1.79 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require("express");
const path = require("path");
const http = require("http");
const https = require("https");
const fs = require("fs");
const port = process.env.PORT || 3001;
const app = express();
const orig_rss_addr = "https://br.pinterest.com";
const orig_img_addr = "https://i.pinimg.com";
app.use(express.static(path.join(__dirname, "static")));
app.use(
"/cache-rss/:username/:board.rss",
// simplest non-caching "GET" proxy for the RSS feeds coming from Pinterest
(req, res, next) => {
if (req.method !== "GET") {
return next();
}
console.log(`request: ${req.params.username}/${req.params.board}`);
https.get(`${orig_rss_addr}/${req.params.username}/${req.params.board}.rss`, (resp) => {
resp.pipe(res);
});
}
);
app.use(
"/cache-images",
express.static(path.join(__dirname, 'cache-images')),
(req, res, next) => {
if (req.method !== "GET") {
return next();
}
console.log(req.url);
const filepath = path.join(__dirname, "cache-images", req.url);
const filedir = path.dirname(filepath);
https.get(`${orig_img_addr}${req.url}`, (resp) => {
if (resp.statusCode == 200)
{
console.log(`writing cache file at ${filepath}`);
fs.mkdirSync(filedir, { recursive: true });
let file = fs.createWriteStream(filepath);
resp.pipe(file);
file.on("finish", function () {
file.close();
});
} else {
console.error(`${req.url} returned status code ${resp.statusCode}`);
res.statusCode = resp.statusCode
}
resp.pipe(res);
})
}
);
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "static/index.html"));
});
const server = http.createServer(app);
server.listen(port, () => { console.log(`Listening on port ${port}`); });