Spaces:
Sleeping
Sleeping
Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const fs = require('fs');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
const PORT = 7860;
|
| 6 |
+
|
| 7 |
+
app.all('/', async (req, res) => {
|
| 8 |
+
res.send(`<!DOCTYPE html>
|
| 9 |
+
<html lang="id">
|
| 10 |
+
<head>
|
| 11 |
+
<meta charset="UTF-8">
|
| 12 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 13 |
+
<title>Speed Test</title>
|
| 14 |
+
</head>
|
| 15 |
+
<body>
|
| 16 |
+
<h1>Speed Test Download</h1>
|
| 17 |
+
<button onclick="startTest()">Mulai Tes</button>
|
| 18 |
+
<p id="result"></p>
|
| 19 |
+
|
| 20 |
+
<script>
|
| 21 |
+
async function startTest() {
|
| 22 |
+
const url = "/static/100MB.bin";
|
| 23 |
+
const startTime = performance.now();
|
| 24 |
+
const response = await fetch(url);
|
| 25 |
+
const reader = response.body.getReader();
|
| 26 |
+
let received = 0;
|
| 27 |
+
|
| 28 |
+
while (true) {
|
| 29 |
+
const { done, value } = await reader.read();
|
| 30 |
+
if (done) break;
|
| 31 |
+
received += value.length;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
const endTime = performance.now();
|
| 35 |
+
const duration = (endTime - startTime) / 1000;
|
| 36 |
+
const speedBps = (received * 8) / duration;
|
| 37 |
+
let speed, unit;
|
| 38 |
+
|
| 39 |
+
if (speedBps >= 1e12) {
|
| 40 |
+
speed = speedBps / 1e12;
|
| 41 |
+
unit = "Tbps";
|
| 42 |
+
} else if (speedBps >= 1e9) {
|
| 43 |
+
speed = speedBps / 1e9;
|
| 44 |
+
unit = "Gbps";
|
| 45 |
+
} else if (speedBps >= 1e6) {
|
| 46 |
+
speed = speedBps / 1e6;
|
| 47 |
+
unit = "Mbps";
|
| 48 |
+
} else {
|
| 49 |
+
speed = speedBps / 1e3;
|
| 50 |
+
unit = "Kbps";
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
document.getElementById("result").textContent = \`Kecepatan: \${speed.toFixed(2)} \${unit}\`;
|
| 54 |
+
}
|
| 55 |
+
</script>
|
| 56 |
+
</body>
|
| 57 |
+
</html>`) });
|
| 58 |
+
|
| 59 |
+
app.use('/static', express.static('static'));
|
| 60 |
+
|
| 61 |
+
app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}/static/100MB.bin`));
|