(Node.js port) Added possibility to act as frontend and backend in Multiple test points mode (#276)

* Added cors middleware to use node version as backend with multiple servers

* Added multiple test point example
This commit is contained in:
Maksim Nagaev 2020-02-20 09:31:04 +03:00 committed by GitHub
parent 9395b08867
commit dd00ef392d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 238 additions and 4 deletions

View file

@ -4,6 +4,11 @@ This is a nodeJS Port from the original PHP-based [LibreSpeed](https://github.co
## Installation
For any instructions and support please check out the [Wiki](https://github.com/librespeed/speedtest/wiki)
##Multiple test points (Node.js port)
You can use any combinations of php and node.js backends, read more at [Wiki](https://github.com/librespeed/speedtest/wiki/Multiple-test-points-(PHP\))
Rename src/public/example-multipleServers-pretty.html to src/public/index.html if you want to use multiple test points mode.
## Support
If you like this project and want to support our work you can either do pull requests or donate via [PayPal](https://paypal.me/dunklesToast)

View file

@ -8,6 +8,7 @@
"private": false,
"dependencies": {
"express": "^4.16.3",
"cors": "^2.8.5",
"gps-distance": "^0.0.4",
"random-bytes": "^1.0.0",
"request": "^2.85.0"

View file

@ -4,18 +4,21 @@ const randomBytes = require('random-bytes');
const path = require('path');
const request = require('request');
const helpers = require('./Helpers');
const cors = require('cors');
let cache;
Server.use(cors());
Server.get('/empty', function (req, res) {
res.sendStatus(200);
res.status(200).send('');
});
Server.post('/empty', function (req, res) {
res.set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
res.set("Cache-Control", "post-check=0, pre-check=0");
res.set("Pragma", "no-cache");
res.sendStatus(200);
res.status(200).send('');
});
Server.get('/garbage', function (req, res) {
@ -27,13 +30,13 @@ Server.get('/garbage', function (req, res) {
res.set('Cache-Control', 'post-check=0, pre-check=0', false);
res.set('Pragma', 'no-cache');
const requestedSize = (req.query.ckSize || 100);
const send = () => {
for (let i = 0; i < requestedSize; i++)
res.write(cache);
res.end();
}
if (cache) {
send();
} else {

View file

@ -0,0 +1,225 @@
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.ico">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
<title>LibreSpeed Example</title>
<script type="text/javascript" src="speedtest.js"></script>
<script type="text/javascript">
//LIST OF TEST SERVERS. See documentation for details if needed
var SPEEDTEST_SERVERS=[
{ //this is node server example
name:"Local dev server", //user friendly name for the server
server:"//127.0.0.1:8888/", //URL to the server. // at the beginning will be replaced with http:// or https:// automatically
dlURL:"garbage", //path to download test on this server (garbage.php or replacement)
ulURL:"empty", //path to upload test on this server (empty.php or replacement)
pingURL:"empty", //path to ping/jitter test on this server (empty.php or replacement)
getIpURL:"getIP" //path to getIP on this server (getIP.php or replacement)
},
{ //this is php server example
name:"Old Speedtest Demo Server",
server:"//mpotdemo.fdossena.com/",
dlURL:"garbage.php",
ulURL:"empty.php",
pingURL:"empty.php",
getIpURL:"getIP.php"
}
//add other servers here, comma separated
];
//INITIALIZE SPEEDTEST
var s=new Speedtest(); //create speedtest object
s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers
s.onupdate=function(data){ //callback to update data in UI
I("ip").textContent=data.clientIp;
I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
I("pingText").textContent=data.pingStatus;
I("jitText").textContent=data.jitterStatus;
}
s.onend=function(aborted){ //callback for test ended/aborted
I("startStopBtn").className=""; //show start button again
if(aborted){ //if the test was aborted, clear the UI and prepare for new test
initUI();
}
}
function selectServer(){ //called when the page is fully loaded
I("startStopBtn").style.display="none"; //hide start/stop button during server selection
s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
I("startStopBtn").style.display=""; //show start/stop button again
I("serverId").textContent=server.name; //show name of test server
});
}
function startStop(){ //start/stop button pressed
if(s.getState()==3){
//speedtest is running, abort
s.abort();
}else{
//test is not running, begin
s.start();
I("startStopBtn").className="running";
}
}
//function to (re)initialize UI
function initUI(){
I("dlText").textContent="";
I("ulText").textContent="";
I("pingText").textContent="";
I("jitText").textContent="";
I("ip").textContent="";
}
function I(id){return document.getElementById(id);}
</script>
<style type="text/css">
html,body{
border:none; padding:0; margin:0;
background:#FFFFFF;
color:#202020;
}
body{
text-align:center;
font-family:"Roboto",sans-serif;
}
h1{
color:#404040;
}
#startStopBtn{
display:inline-block;
margin:0 auto;
color:#6060AA;
background-color:rgba(0,0,0,0);
border:0.15em solid #6060FF;
border-radius:0.3em;
transition:all 0.3s;
box-sizing:border-box;
width:8em; height:3em;
line-height:2.7em;
cursor:pointer;
box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
}
#startStopBtn:hover{
box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
}
#startStopBtn.running{
background-color:#FF3030;
border-color:#FF6060;
color:#FFFFFF;
}
#startStopBtn:before{
content:"Start";
}
#startStopBtn.running:before{
content:"Abort";
}
#test{
margin-top:2em;
margin-bottom:12em;
}
div.testArea{
display:inline-block;
width:14em;
height:9em;
position:relative;
box-sizing:border-box;
}
div.testName{
position:absolute;
top:0.1em; left:0;
width:100%;
font-size:1.4em;
z-index:9;
}
div.meterText{
position:absolute;
bottom:1.5em; left:0;
width:100%;
font-size:2.5em;
z-index:9;
}
#dlText{
color:#6060AA;
}
#ulText{
color:#309030;
}
#pingText,#jitText{
color:#AA6060;
}
div.meterText:empty:before{
color:#505050 !important;
content:"0.00";
}
div.unit{
position:absolute;
bottom:2em; left:0;
width:100%;
z-index:9;
}
div.testGroup{
display:inline-block;
}
@media all and (max-width:65em){
body{
font-size:1.5vw;
}
}
@media all and (max-width:40em){
body{
font-size:0.8em;
}
div.testGroup{
display:block;
margin: 0 auto;
}
}
</style>
</head>
<body>
<h1>LibreSpeed Example</h1>
<div id="startStopBtn" onclick="startStop()"></div>
<div id="serverId">Selecting server...</div>
<div id="test">
<div class="testGroup">
<div class="testArea">
<div class="testName">Download</div>
<div id="dlText" class="meterText"></div>
<div class="unit">Mbps</div>
</div>
<div class="testArea">
<div class="testName">Upload</div>
<div id="ulText" class="meterText"></div>
<div class="unit">Mbps</div>
</div>
</div>
<div class="testGroup">
<div class="testArea">
<div class="testName">Ping</div>
<div id="pingText" class="meterText"></div>
<div class="unit">ms</div>
</div>
<div class="testArea">
<div class="testName">Jitter</div>
<div id="jitText" class="meterText"></div>
<div class="unit">ms</div>
</div>
</div>
<div id="ipArea">
IP Address: <span id="ip"></span>
</div>
</div>
<a href="https://github.com/librespeed/speedtest">Source code</a>
<script type="text/javascript">
initUI();
selectServer();
</script>
</body>
</html>