forked from web-standards-ru/calendar-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.mjs
56 lines (45 loc) · 1.31 KB
/
server.mjs
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
import express from 'express';
import scanEvent from './helpers/scan_event.mjs';
const { API_TOKEN, PORT = 3000 } = process.env;
if (!API_TOKEN) {
throw new Error('Not set env API_TOKEN');
}
/**
* Создание HTTP-сервера.
*
* @returns {{ server: express.server, app: express.Express}} - Созданный инстанс HTTP-сервера.
*/
export default function () {
const app = express();
app.disable('x-powered-by');
app.use((req, _, next) => {
console.log(req.method, req.url, req.header('x-real-ip') || req.ip);
next();
});
app.get('/', async (req, res, next) => {
if (req.query.token !== API_TOKEN) {
return res.status(400).end('Error token');
}
try {
res.json(await scanEvent());
} catch (exc) {
next(exc);
}
});
app.use((req, res) => {
console.warn('Page not found', req.url);
return res.status(404).end('Page not found');
});
app.use((err, _, res) => {
console.error(err.stack);
return res.status(500).end('Server error');
});
return {
server: app.listen(PORT, () => {
console.log(
`App started and available at http://localhost:${PORT}`,
);
}),
app,
};
}