-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (34 loc) · 1.25 KB
/
Copy pathserver.js
File metadata and controls
45 lines (34 loc) · 1.25 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
const http = require('http');
const steamAPI = require('./api/index');
const PORT = process.env.PORT || 3000; // port
const STEAM_API_KEY = process.env.STEAM_API_KEY; // your steam api key
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
};
const server = http.createServer(async (req, res) => {
const baseURL = req.protocol + '://' + req.headers.host + '/';
const reqURL = new URL(req.url, baseURL);
if (req.method === 'GET') {
const url = steamAPI.getURL(reqURL.pathname, reqURL.search, STEAM_API_KEY);
if (typeof url !== 'string') {
res.writeHead(404, headers);
res.end(JSON.stringify(url));
return;
}
const response = await fetch(url);
if (!response.ok) {
res.writeHead(response.status, headers);
res.end(JSON.stringify({ message: response.statusText }));
return;
}
const data = await response.json();
res.writeHead(200, headers);
res.end(JSON.stringify(data));
return;
}
res.writeHead(405, headers);
res.end(`${req.method} is not allowed for the request.`);
});
server.listen(PORT, () => console.log(`server listening on port: ${PORT}`));