-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (62 loc) · 2.5 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
71
72
73
74
// Dependencies
const express = require('express');
const fetch = require('node-fetch');
const lib = require("./lib");
// Create Express sever
const app = express();
// JSON request settings
const settings = { method: "Get" };
// Use body-parser and JSON in the express app
app.use(express.urlencoded({ extended: true }))
app.use(express.json());
// Serve everything in the public directory
app.use(express.static(__dirname + '/public'));
// Use ejs as the view engine
app.set('view engine', 'ejs');
lib.createFile("./searches")
var searches = lib.readFile("./searches");
//--- ROUTING REQUESTS ---//
// Home page
app.get(["/", "/home"], (req, res) => res.render('index'));
// Profile pages
app.get('/profile/:id', (req, res) => {
searches++;
lib.writeFile("./searches", searches.toString());
// Check if ID is a valid Steam account
fetch(`http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=${process.env.API_KEY}&steamids=${req.params.id}`, settings)
.then(res => res.json())
.then((json) => {
if (json.response.players.length != 0) {
// Fetch user stats JSON if it is
lib.fetchJson(req.params.id, req, res, json.response.players[0]["avatarfull"], json.response.players[0]["personaname"]);
}
else {
// If it isn't a valid ID, check if it is a valid vanity URL
fetch(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${process.env.API_KEY}&vanityurl=${req.params.id}`, settings)
.then(res => res.json())
.then((json) => {
if (json.response.success == 1) {
// Fetch JSON using vanity URL's ID
fetch(`http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=${process.env.API_KEY}&steamids=${json.response.steamid}`, settings)
.then(res => res.json())
.then((infoJson) => {
lib.fetchJson(json.response.steamid, req, res, infoJson.response.players[0]["avatarfull"], infoJson.response.players[0]["personaname"]);
});
}
else {
// User didn't submit a valid account, send 404 page
res.render('profile_not_found');
}
});
}
});
});
// About page
app.get("/about", (req, res) => res.render('about'));
// Catch all other pages and serve 404 error page
app.get('*', function (req, res) {
res.status(404).render('404');
});
// Start Express server
app.listen(process.env.PORT || 5000);
console.log("Listening at http://127.0.0.1:5000");