Skip to content
Related Articles
Open in App
Not now

Related Articles

How to test internet speed using Node.js ?

Improve Article
Save Article
Like Article
  • Last Updated : 17 Mar, 2022
Improve Article
Save Article
Like Article

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.


const express = require('express');
const app = express();
app.get('/' , (req , res)=>{
// Server setup
app.listen(4000 , ()=>{
    console.log("server is running on port 4000");

Output: We will get the following output on the browser screen.


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.


<!DOCTYPE html>
<html lang="en">
    <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>
      <h1 style="color: green">GeeksforGeeks</h1>
      <form method="POST" action="/test">
        <button>Check Internet Speed</button>


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"/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 : ${}</h2>
                    <h2>Download Speed : ${}</h2>
                    <h2>Upload Speed : ${result.upload}</h2>
// 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.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!