-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (40 loc) · 1.26 KB
/
server.js
File metadata and controls
48 lines (40 loc) · 1.26 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
const path = require('path');
const express = require('express');
require('dotenv').config({path: path.join(__dirname, '.env')});
const app = express();
const {PORT, API} = process.env;
const fetch = async (...props) => {
const {default: nodeFetch} = await import('node-fetch');
return nodeFetch(...props);
};
const handleRequest = async (req, res) => {
try {
const url = `${API}${req.url.substring(1)}`;
const {body} = req;
const Authorization = req.get('Authorization');
const response = await fetch(url, {
method: req.method,
body: body && Object.entries(body).length > 0 ? JSON.stringify(body) : null,
headers: {
Authorization,
'Content-Type': 'application/json'
}
});
res.send(await response.json());
} catch (err) {
res.status(500).send({
error: true,
message: 'Internal server error'
});
}
};
app.use(express.static(__dirname));
app.use(express.json({}));
app.use(express.urlencoded({extended: true}));
app.get('/status', handleRequest);
app.post('/public-api/login', handleRequest);
app.post('/public-api/session', handleRequest);
app.get('*', (_, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(PORT, () => console.log(`App running at port ${PORT}`));