-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (43 loc) · 1.2 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
const express = require("express");
const axios = require("axios");
const validator = require("validator");
const app = express();
// middleware
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// view engine
app.set("view engine", "ejs");
// routes
app.get("/", (req,res)=>{
res.render("home");
});
app.get("/track", (req,res) => {
res.render("track", { data: "" });
})
app.post("/track", async (req,res) => {
let platform = validator.escape(req.body.platform);
let username = validator.escape(req.body.username);
let response;
try {
response = await axios.get(`https://competitive-coding-api.herokuapp.com/api/${platform}/${username}`);
} catch (err) {
if(err.response.status === 500) {
response = {
data: {
status: "Failed",
status_code: 500,
details: "Internal server error"
}
}
}
}
res.render("track", { data: {
username: username,
platform: platform,
response: response.data
}});
})
app.listen(3000, ()=>{
console.log('server is running...');
});