-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (55 loc) · 1.55 KB
/
index.js
File metadata and controls
69 lines (55 loc) · 1.55 KB
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
const express = require("express");
const axios = require("axios");
const path = require("path");
const compression = require("compression");
const helmet = require("helmet");
const port = process.env.PORT || 3000;
const app = express();
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(helmet.hidePoweredBy());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(compression());
app.get("/", (req, res) => {
res.render("index", { data: false });
});
app.post("/", async (req, res, next) => {
const { user } = req.body;
let challenges = {};
await axios
.get(
`https://www.codewars.com/api/v1/users/${user}/code-challenges/completed?page=0`,
)
.then(response => {
challenges = response.data.data;
})
.catch(error => {
next(error);
});
axios
.get(`https://www.codewars.com/api/v1/users/${user}`)
.then(response => {
const { languages } = response.data.ranks;
res.render("index", {
data: response.data,
languages: Object.entries(languages),
challenges: challenges,
});
})
.catch(error => {
next(error);
});
});
app.all("*", (req, res) => {
res.status(404).send("Page Not Found.");
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
res.status(err.status || 500);
res.render("error");
});
app.listen(port);
module.exports = app;