javascript - Node.js and SQL to check a database for results -


i have challenge had done php, wanted see how done via node.js , sql.

i beginner this, , not sure @ if did legal, wanting learn in node; challenge this:

we function takes in useragent of browser string, , compares list of approved browsers , versions

the database structure looks this:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| bigintpkid | varchar(50) browsername | varchar(50) versionnumber | bitgreaterversions |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|         1       |           internet explorer        |                       9                     |             false          |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|         1       |           internet explorer        |                       10                   |             false          |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|         1       |           chrome                      |                       26                   |             true           |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|         1       |           firefox                        |                       22                   |             true           |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bitgreaterversions version greater version number compatible.

data table expressed in human readable format “our app compatible ie9,10, chrome 26+ , firefox 22+”

create function take user agent string, , return true or false if compatible based on above criteria.

and here node , sql code (i hope)...

var express = require("express"); var mysql = require("mysql");  var connection = mysql.createconnection({     host: 'localhost',     username: 'username',     password: 'password',     dbname: 'browsers' });  var app = express();  // if there error connecting database connection.connect(function(error) {     if(error) {         console.log('cannot establish connection database...');         return;     }     console.log('database connection established...'); });  // function check browser compatibility function getuseragent(useragent, versionnumber) {      var isbrowsercompatible;      // access database     app.get("/path-to-database", function(req, res) {         // query required results         connection.query('select browsername, versionnumber, bitgreaterversions useragenttable', function(error, rows, fields) {         connection.end();              if(error) {                 console.log('cannot find results...');             }              isbrowsercompatible = false;              // loop on returned table values             for(var = 0; < rows.length; i++) {                 // if browser name , version number equal useragent , versionnumber                 if(rows[i].browsername == useragent && rows[i].versionnumber == versionnumber) {                     // browser compatible                     isbrowsercompatible = true;                     break;                   // if browser name equal useragent version number less versionnumber                 } else if(rows[i].browsername == useragent && rows[i].versionnumber < versionnumber) {                     if(rows[i].bitgreaterversions == 'false') {                         // browser not compatible                         break;                     } else {                         // otherwise browser compatible                         isbrowsercompatible = true;                         break;                     }                 }             }             return isbrowsercompatible;         });     });      return isbrowsercompatible;  } 

hopefully can point me in right direction. i'm looking see if actual function can written legally in way have it, , if not, how go it, logic , all. gentle...

you can use ua-parser-js package parse user agent string.

var parser = require("ua-parser-js"); // somewhere within server request-response cycle // user-agent header  var ua = parser(req.headers["user-agent"]); // ua.getbrowser() returns object browser name , version var details = ua.getbrowser(); // e.g. returns { name: "chrome", version: "52.0.2743.116" } // details.version.split(".") return array of strings // ["52", "0", "2743", "116"] interest in [0] element var browser = details.name; var version = parseint(details.version.split(".")[0]); // 52 

armed browser , version data can query database.

// var mysql = require('mysql'); // etc. etc.  connection.query("select * browsertable browsername = ?", [browser], function(error, rows) {   // handle error   if (error) {     throw error;   }   // process results returned   var iscompatible = false;   (var = 0; < rows[0].length; i++) {     var dbversion = parseint(rows[0][i].versionnumber);     if ( ((rows[0][i].bitgreaterversions == 'true') && (dbversion < version))     || (dbversion == version) ) {       iscompatible = true;       break;     }   }   return iscompatible; }); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -