-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (50 loc) · 1.66 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
require('dotenv').config();
const express = require('express');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const multer = require('multer');
const upload = multer();
const fs = require('fs');
const pxls = require('./handlers/pxls');
const place = require('./handlers/place');
const app = express();
app.use(cookieParser());
app.use(cors({ credentials: true, origin: process.env.ALLOW_ORIGIN }));
const requestFiles = fs.readdirSync('./requests').filter(file => file.endsWith('.js'));
const getRequests = {};
const postRequests = {};
const putRequests = {};
const patchRequests = {};
const deleteRequests = {};
for (const file of requestFiles) {
const request = require(`./requests/${file}`);
if (request.type === 'get') getRequests[request.name] = request;
if (request.type === 'post') postRequests[request.name] = request;
if (request.type === 'put') putRequests[request.name] = request;
if (request.type === 'patch') patchRequests[request.name] = request;
if (request.type === 'delete') deleteRequests[request.name] = request;
}
app.get('/api/:request', async (req, res) => {
if (Object.keys(getRequests).includes(req.params.request)) {
try {
await getRequests[req.params.request].execute(req, res);
} catch (e) {
res.status(500).send(e);
}
}
});
app.post('/api/:request', upload.single('image'), async (req, res) => {
if (Object.keys(postRequests).includes(req.params.request)) {
try {
await postRequests[req.params.request].execute(req, res);
} catch (e) {
console.error(e);
res.status(500).send(e);
}
}
});
app.listen(1677, () => {
console.log('API listening on port 1677...');
pxls.init();
place.connect();
});