speedtest / server.js
fast72's picture
Create server.js
e8a6fe6 verified
raw
history blame
1.84 kB
const express = require('express');
const fs = require('fs');
const app = express();
const PORT = 7860;
app.all('/', async (req, res) => {
res.send(`<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speed Test</title>
</head>
<body>
<h1>Speed Test Download</h1>
<button onclick="startTest()">Mulai Tes</button>
<p id="result"></p>
<script>
async function startTest() {
const url = "/static/100MB.bin";
const startTime = performance.now();
const response = await fetch(url);
const reader = response.body.getReader();
let received = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
received += value.length;
}
const endTime = performance.now();
const duration = (endTime - startTime) / 1000;
const speedBps = (received * 8) / duration;
let speed, unit;
if (speedBps >= 1e12) {
speed = speedBps / 1e12;
unit = "Tbps";
} else if (speedBps >= 1e9) {
speed = speedBps / 1e9;
unit = "Gbps";
} else if (speedBps >= 1e6) {
speed = speedBps / 1e6;
unit = "Mbps";
} else {
speed = speedBps / 1e3;
unit = "Kbps";
}
document.getElementById("result").textContent = \`Kecepatan: \${speed.toFixed(2)} \${unit}\`;
}
</script>
</body>
</html>`) });
app.use('/static', express.static('static'));
app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}/static/100MB.bin`));