Node.js和SQL检查数据库的结果

时间:2016-11-17 19:21:19

标签: javascript mysql node.js express

我有使用PHP完成的这项挑战,但希望了解如何通过node.js和SQL完成。

我是这个的初学者,并且根本不确定我甚至做了什么是合法的,但我想学习在节点中做到这一点;挑战是:

  

我们想要一个将浏览器的UserAgent作为字符串接收的函数,并将其与已批准的浏览器和版本列表进行比较

     

数据库结构看起来如下:
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | bigIntPKID | varchar(50)BrowserName | varchar(50)VersionNumber | bitGreaterVersions |
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | 1 | Internet Explorer | 9 |假|
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | 1 | Internet Explorer | 10 |假|
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | 1 | Chrome | 26 |真的|
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | 1 | Firefox | 22 |真的|
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  bitGreaterVersions是大于版本号兼容的任何版本。

  以人类可读格式表示的数据表将是“我们的应用程序与IE9,10,Chrome 26+和Firefox 22+兼容”   

  创建一个将用户代理作为字符串的函数,如果它基于上述条件兼容,则返回true或false。

然后这是节点和SQL代码(我希望)......

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 is an error connecting to the database
connection.connect(function(error) {
    if(error) {
        console.log('Cannot establish connection to database...');
        return;
    }
    console.log('Database connection established...');
});

// function to check browser compatibility
function getUserAgent(userAgent, versionNumber) {

    var isBrowserCompatible;

    // access the database
    app.get("/path-to-database", function(req, res) {
        // query for the required results
        connection.query('SELECT BrowserName, VersionNumber, bitGreaterVersions FROM userAgentTable', function(error, rows, fields) {
        connection.end();

            if(error) {
                console.log('Cannot find the results...');
            }

            isBrowserCompatible = false;

            // loop over the returned table values
            for(var i = 0; i < rows.length; i++) {
                // if browser name and version number are equal to userAgent and versionNumber
                if(rows[i].BrowserName == userAgent && rows[i].VersionNumber == versionNumber) {
                    // the browser is compatible
                    isBrowserCompatible = true;
                    break;
                  // if browser name is equal to userAgent but the version number is less than versionNumber
                } else if(rows[i].BrowserName == userAgent && rows[i].VersionNumber < versionNumber) {
                    if(rows[i].bitGreaterVersions == 'False') {
                        // the browser is not compatible
                        break;
                    } else {
                        // otherwise the browser is compatible
                        isBrowserCompatible = true;
                        break;
                    }
                }
            }
            return isBrowserCompatible;
        });
    });

    return isBrowserCompatible;

}

希望有人可以指出我正确的方向。我主要是想看看实际功能是否可以按照我拥有它的方式合法编写,如果没有,如何去做,逻辑和所有。要温柔......

1 个答案:

答案 0 :(得分:1)

您可以使用ua-parser-js包来解析用户代理字符串。

var parser = require("ua-parser-js");
// somewhere within the server request-response cycle
// you get user-agent header 
var ua = parser(req.headers["user-agent"]);
// ua.getBrowser() returns an object with the browser name and version
var details = ua.getBrowser();
// e.g. returns { name: "Chrome", version: "52.0.2743.116" }
// details.version.split(".") will return an array of strings
// ["52", "0", "2743", "116"] you are only interest in the [0] element
var browser = details.name;
var version = parseInt(details.version.split(".")[0]); // 52

使用browserversion数据,您可以查询数据库。

// var mysql = require('mysql');
// etc. etc.

connection.query("SELECT * FROM BrowserTable WHERE BrowserName = ?", [browser],
function(error, rows) {
  // handle error
  if (error) {
    throw error;
  }
  // process the results returned
  var isCompatible = false;
  for (var i = 0; i < 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;
});