Get Started
In order to get started we need to install the following dependencies which are as follows:
npm init -y
This will initialize a package.json file for your app which contains the basic information about the app.
npm i express
npm i ejs
npm i request
npm i cric-player-info
Screenshots
Live Demo
You can see the live demo of the app here
Building the App
Now to build the app we need to create the index.js
file and copy paste the following code to start the express server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const express = require('express') const cricData= require('cric-player-info'); const bodyParser = require('body-parser') const app = express() app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) app.set('view engine','ejs') app.get('/',(req,res) =>{ res.render('index') }) app.listen(5000,() =>{ console.log("App is listening on Port 5000") }) |
Now we need to create the views folder and inside this we need to create a file called as index.ejs
and copy paste the following code
views/index.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html> <head> <title>Cricket Player Stats App</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> </head> <style> .ui-autocomplete { max-height: 300px; overflow-y: auto; /* prevent horizontal scrollbar */ overflow-x: hidden; } </style> <body> <div class="container"> <form id="form"> <div class="form-group"> <label for="player">Player Name</label> <input class="form-control" id="playername" placeholder="Enter Player Name" required /> </div> <div class="form-group"> <button class="btn btn-danger btn-block">Get Player Info</button> </div> </form> <div id="result"></div> </div> </body> </html> |
So now after this we need to inject the player names when the user types inside the input field making a autocomplete suggestion field. For this we will be making use of AJAX and we will be making several ajax calls to the express server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
<script> var playerNames = []; var result; var inputplayer; $("#playername").on("keyup", function () { inputplayer = $(this).val(); $.ajax({ method: "POST", url: "/getplayernames", data: { input: $(this).val() }, success: function (data) { //console.log(data.names) playerNames = data.names; let match = playerNames.filter((name) => { const regex = new RegExp(`^${inputplayer}`, "gi"); return name.match(regex); }); if (inputplayer.length === 0) { playerNames = []; } $("#playername").autocomplete({ source: match, }); }, }); }); $("#playername").on("change", function () { inputplayer = $(this).val(); }); $("#form").submit(function (e) { e.preventDefault(); $("#button").text("Fetching Player Stats") $("#button").prop("disabled","true") $("#result").empty() $.ajax({ method: "POST", url: "/getplayerinfo", data: { playername: inputplayer }, success: function (data) { console.log(data.info); if(data.info == "Player not found") { $("#playername").val("") $("#button").removeAttr("disabled"); $("#button").text("Get Player Info") swal("Error", "Player Name Doesn't Found", "error"); }else{ $("#playername").val("") $("#button").removeAttr("disabled"); $("#button").text("Get Player Info") result = ` <table class="table table-striped"> <thead> <tr> <th>Name</th> <th>Date of Birth (Age)</th> <th>Birth Place</th> <th>Country</th> </tr> </thead> <tbody> <tr> <td>${data.info.name}</td> <td>${data.info.dateOfBirth}</td> <td>${data.info.birthPlace}</td> <td>${data.info.country}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>Test Debut</th> <th>ODI Debut</th> <th>T20I Debut</th> <th>IPL Debut</th> </tr> </thead> <tbody> <tr> <td>${data.info.testDebut}</td> <td>${data.info.odiDebut}</td> <td>${data.info.t20iDebut}</td> <td>${data.info.iplDebut}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>Last Test</th> <th>Last ODI</th> <th>Last T20I</th> <th>Last IPL Match</th> </tr> </thead> <tbody> <tr> <td>${data.info.lastTest}</td> <td>${data.info.lastOdi}</td> <td>${data.info.lastT20i}</td> <td>${data.info.lastIpl}</td> </tr> </tbody> </table> <br> <h4 class="text-center">Batting Statistics</h4> <br> <table class="table table-striped"> <thead> <tr> <th>No of Test Matches</th> <th>Test Runs</th> <th>Average</th> <th>Strike Rate</th> <th>No of 50's</th> <th>No of 100's</th> <th>High Score</th> <th>No of Fours</th> <th>No of Sixes</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_test_matches}</td> <td>${data.info.batting_test_runs}</td> <td>${data.info.batting_test_average}</td> <td>${data.info.batting_test_strikeRate}</td> <td>${data.info.batting_test_fifties}</td> <td>${data.info.batting_test_hundreds}</td> <td>${data.info.batting_test_highScore}</td> <td>${data.info.batting_test_fours}</td> <td>${data.info.batting_test_sixes}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>No of ODI Matches</th> <th>ODI Runs</th> <th>Average</th> <th>Strike Rate</th> <th>No of 50's</th> <th>No of 100's</th> <th>High Score</th> <th>No of Fours</th> <th>No of Sixes</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_odi_matches}</td> <td>${data.info.batting_odi_runs}</td> <td>${data.info.batting_odi_average}</td> <td>${data.info.batting_odi_strikeRate}</td> <td>${data.info.batting_odi_fifties}</td> <td>${data.info.batting_odi_hundreds}</td> <td>${data.info.batting_odi_highScore}</td> <td>${data.info.batting_odi_fours}</td> <td>${data.info.batting_odi_sixes}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>No of T20I Matches</th> <th>T20I Runs</th> <th>Average</th> <th>Strike Rate</th> <th>No of 50's</th> <th>No of 100's</th> <th>High Score</th> <th>No of Fours</th> <th>No of Sixes</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_t20i_matches}</td> <td>${data.info.batting_t20i_runs}</td> <td>${data.info.batting_t20i_average}</td> <td>${data.info.batting_t20i_strikeRate}</td> <td>${data.info.batting_t20i_fifties}</td> <td>${data.info.batting_t20i_hundreds}</td> <td>${data.info.batting_t20i_highScore}</td> <td>${data.info.batting_t20i_fours}</td> <td>${data.info.batting_t20i_sixes}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>No of IPL Matches</th> <th>IPL Runs</th> <th>Average</th> <th>Strike Rate</th> <th>No of 50's</th> <th>No of 100's</th> <th>High Score</th> <th>No of Fours</th> <th>No of Sixes</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_ipl_matches}</td> <td>${data.info.batting_ipl_runs}</td> <td>${data.info.batting_ipl_average}</td> <td>${data.info.batting_ipl_strikeRate}</td> <td>${data.info.batting_ipl_fifties}</td> <td>${data.info.batting_ipl_hundreds}</td> <td>${data.info.batting_ipl_highScore}</td> <td>${data.info.batting_ipl_fours}</td> <td>${data.info.batting_ipl_sixes}</td> </tr> </tbody> </table> <br> <h4 class="text-center">Bowling Statistics</h4> <br> <table class="table table-striped"> <thead> <tr> <th>No of Test Matches</th> <th>Test Wickets</th> <th>Average</th> <th>Strike Rate</th> <th>Economy</th> <th>No of 5 Wickets Hauls</th> <th>Best Performance</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_test_matches}</td> <td>${data.info.bowling_test_bestBowlinginInnings}</td> <td>${data.info.bowling_test_strikeRate}</td> <td>${data.info.bowling_test_fiveWickets}</td> <td>${data.info.bowling_test_average}</td> <td>${data.info.bowling_test_tenWickets}</td> <td>${data.info.bowling_test_bestBowlinginMatch}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>No of ODI Matches</th> <th>ODI Wickets</th> <th>Average</th> <th>Strike Rate</th> <th>Economy</th> <th>No of 5 Wickets Hauls</th> <th>Best Performance</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_odi_matches}</td> <td>${data.info.bowling_odi_bestBowlinginInnings}</td> <td>${data.info.bowling_odi_strikeRate}</td> <td>${data.info.bowling_odi_fiveWickets}</td> <td>${data.info.bowling_odi_average}</td> <td>${data.info.bowling_odi_tenWickets}</td> <td>${data.info.bowling_odi_bestBowlinginMatch}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>No of T20I Matches</th> <th>T20I Wickets</th> <th>Average</th> <th>Strike Rate</th> <th>Economy</th> <th>No of 5 Wickets Hauls</th> <th>Best Performance</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_t20i_matches}</td> <td>${data.info.bowling_t20i_bestBowlinginInnings}</td> <td>${data.info.bowling_t20i_strikeRate}</td> <td>${data.info.bowling_t20i_fiveWickets}</td> <td>${data.info.bowling_t20i_average}</td> <td>${data.info.bowling_t20i_tenWickets}</td> <td>${data.info.bowling_t20i_bestBowlinginMatch}</td> </tr> </tbody> </table> <table class="table table-striped"> <thead> <tr> <th>No of IPL Matches</th> <th>IPL Wickets</th> <th>Average</th> <th>Strike Rate</th> <th>Economy</th> <th>No of 5 Wickets Hauls</th> <th>Best Performance</th> </tr> </thead> <tbody> <tr> <td>${data.info.batting_ipl_matches}</td> <td>${data.info.bowling_ipl_bestBowlinginInnings}</td> <td>${data.info.bowling_ipl_strikeRate}</td> <td>${data.info.bowling_ipl_fiveWickets}</td> <td>${data.info.bowling_ipl_average}</td> <td>${data.info.bowling_ipl_tenWickets}</td> <td>${data.info.bowling_ipl_bestBowlinginMatch}</td> </tr> </tbody> </table> `; $("#result").append(result) } }, }); }); </script> |
DOWNLOAD SOURCE CODE