-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
60 lines (48 loc) · 1.45 KB
/
app.ts
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
60
import express, { Application, Request, Response } from 'express';
import { createServer } from 'node:http';
import { join } from 'node:path';
import { Server } from 'socket.io';
import favicon from 'serve-favicon';
import rateLimit, { RateLimitRequestHandler } from 'express-rate-limit';
import { DataClient } from './cosmos'
import 'dotenv/config'
const app: Application = express();
const server = createServer(app);
const io = new Server(server, {
transports: ['websocket', 'polling'],
cors: {
origin: '*',
methods: ['GET', 'POST']
},
});
const limiter: RateLimitRequestHandler = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
app.get('/', limiter, (_: Request, res: Response) => {
res.sendFile(join(__dirname, 'static', 'index.html'));
});
app.use(
favicon(join(__dirname, 'static', 'favicon.ico'))
);
app.use(express.static('static'));
io.on('connection', (socket) => {
console.log(`Connected: ${socket.id}`);
socket.on('start', async (_) => {
console.log('Started');
await new DataClient().start((message: string) => {
console.log(message);
io.emit('new_message', message);
});
});
});
io.on('error', (_, error) => {
console.log(`Error: ${error}`);
});
io.on('disconnect', (_, reason) => {
console.log(`Disconnected: ${reason}`);
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server running: \\:${port}`);
});