-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (60 loc) · 1.59 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
62
63
64
65
66
67
68
69
70
import express from "express";
import getParameterObj from "./lib/utils.js";
import Memes from "./lib/memes.js";
import Reddit from "./lib/reddit.js";
import cors from "cors";
import packageJSON from "./package.json" assert { type: "json" };
const app = express();
const port = process.env.PORT || 4000;
app.use(
cors({
origin: "*",
})
);
app.use("/favicon.ico", express.static("public/images/icon.png"));
app.get(
"/memes",
(req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET");
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
);
res.setHeader("Access-Control-Allow-Credentials", true);
var subreddits = req.query.subreddits;
var max = req.query.max;
var filter = req.query.filter;
var send = false;
var parameters = getParameterObj(subreddits, max, filter);
var meme = new Memes(parameters);
meme.getMultipleSubreddits((data) => {
if (send) return;
send = true;
res.status(200).send(data);
}, parameters.lastIDs);
},
cors()
);
app.get("/reddit/u/:user", (req, res) => {
if (req.params.user) {
var user = req.params.user;
Reddit.getUserInfo(user).then((data) => {
res.send({ success: true, data: data });
});
} else {
res.send({
success: false,
error: "No user specified!",
});
}
});
app.get("/", (req, res) => {
var repoUrl = packageJSON.repository.url.startsWith("git+")
? packageJSON.repository.url.slice(4)
: packageJSON.repository.url;
res.redirect(repoUrl);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});