-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
35 lines (29 loc) · 947 Bytes
/
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
const express = require('express');
const app = express();
const port = process.env.PORT || 8088;
const path = require('path');
const dotenv = require('dotenv');
dotenv.config();
app.use(express.static('dist'));
app.listen(port);
app.use(express.static(__dirname + '/dist'));
app.get('/api', async (req, res) => {
const apiUrl = process.env.API_URL || 'https://frontend-api.spore.ws/api';
try {
const query = req.query.q;
const response = await fetch(query ? `${apiUrl}?q=${query}` : apiUrl);
const contentType = response.headers.get('content-type');
if (contentType.includes('text/html')) {
const text = await response.text();
return res.send(text);
} else {
const data = await response.json();
return res.json(data);
}
} catch (error) {
return res.status(500).send(error);
}
});
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, './dist/index.html'));
});