How to test internet speed using Node.js ?
The following approach covers how to do an internet speed test in NodeJs. We will use the speed-test node-package to achieve so. This package will help us for getting the current download and upload speed.
Use the following steps to install the module and do a speed test in node.js:
Step 1: Creating a directory for our project and making that our working directory.
$ mkdir speed-test-gfg $ cd speed-test-gfg
Step 2: Use the npm init command to create a package.json file for our project.
$ npm init // OR $ npm init -y /* For auto add the required field */
Note: Keep pressing enter and enter “yes/no” accordingly at the terminus line.
Step 3: Installing the Express.js and speed-test module. Now in your speed-test-gfg(name of your folder) folder type the following command line:
$ npm install express $ npm install speed-test --global
Note: Install both modules separately.
Step 4: Creating index.js and index.html files, our project structure will look like this.
Step 5: Creating a basic server. Write down the following code in the index.js file.
index.js
const express = require( 'express' ); const app = express(); app.get( '/' , (req , res)=>{ res.send( "GeeksforGeeks" ); }); // Server setup app.listen(4000 , ()=>{ console.log( "server is running on port 4000" ); }); |
Output: We will get the following output on the browser screen.
GeeksforGeeks
Step 6: Now let’s implement the functionality by which we get the current download and upload speed. Here we are using the exec function which is available in child-process to execute the command for the speed test.
index.html
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" /> < meta http-equiv = "X-UA-Compatible" content = "IE=edge" /> < meta name = "viewport" content = "width=device-width, initial-scale=1.0" /> < title >Speed Test</ title > </ head > < body > < center > < h1 style = "color: green" >GeeksforGeeks</ h1 > < form method = "POST" action = "/test" > < button >Check Internet Speed</ button > </ form > </ center > </ body > </ html > |
Javascript
const express = require( "express" ); const app = express(); const { exec } = require( "child_process" ); // Home Route app.get( "/" , (req, res) => { res.sendFile(__dirname + "/views/index.html" ); }); // Speed Test app.post( "/test" , (req, res) => { exec( "speed-test --json" , (err, stdout, stderr) => { if (err || stderr) return res.send( "Error while testing internet speed." ); const result = JSON.parse(stdout); const response = `<center> <h2>Ping : ${result.ping}</h2> <h2>Download Speed : ${result.download}</h2> <h2>Upload Speed : ${result.upload}</h2> </center>`; res.send(response); }); }); // Server app.listen(4000, () => { console.log( "Server running on port - 4000" ); }); |
Step 7: Run the server using the following command.
node index.js
Output: Now open http://localhost:4000 on your browser and wait for 1-2 minutes after clicking the button to see the below output.
Reference: https://www.npmjs.com/package/speed-test
Please Login to comment...