Newer
Older
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
import express from 'express';
import expressWs from 'express-ws';
import path from 'path';
import { runTests } from './testing';
const { app } = expressWs(express());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const resultsCache = {};
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.post('/viewResults', (req, res) => {
const url = req.body.url;
const id = btoa(url);
res.redirect(`/viewResults/${id}`);
});
app.get('/viewResults/:id', (req, res) => {
res.sendFile(path.join(__dirname, 'results.html'));
});
app.ws('/results/:id', async (ws, req) => {
const awsUrl = atob(req.params.id);
const iterations: number = Number(req.query.iterations) || 1;
if (resultsCache[awsUrl]) {
ws.send(JSON.stringify(resultsCache[awsUrl]));
} else {
runTests(awsUrl, resultsCache, iterations, ws);
}
ws.onmessage = (msg) => {
const iterations = Number(msg.data);
runTests(awsUrl, resultsCache, iterations, ws);
};
});
app.listen(80, () => {
console.log('Server is running on port 80');
});