无法将Node.js服务器连接到Azure SQL数据库

无法将Node.js服务器连接到Azure SQL数据库

问题描述:

我正在Heroku上运行一个简单的Node.js服务器.我已经建立了一个Azure SQL数据库,而我只是想从服务器建立到它的连接.我正在使用 tedious.js 进行连接.据我所知,我遵循的是文档中的模式,但是连接没有通过.这是我的代码(更改了用户名和密码).目前,从我的浏览器对"/data"页面的GET请求中调用了connect函数,但是该页面从不加载,连接也从未通过.有指针吗?

I'm running a simple Node.js server on Heroku. I've set up an Azure SQL database and I'm just trying to establish a connection to it from the server. I'm using tedious.js to connect. As far as I can tell, I'm following the patterns in the docs, but the connection doesn't go through. This is the code I have (altered username and password). For now, the connect function is called upon a GET request to the "/data" page from my browser, but the page never loads and the connection never goes through. Any pointers?

var azure = require("azure-storage");

var Connection = require("tedious").Connection;

var config = {
  Server : "cultureofthefewpractice.database.windows",
  username : "XXXXX",
  password : "XXXXX",
  options : {
    port: 1433,
    Database : "cultureofthefewpracticedatabase",
    connectTimeout : 3000,
  },
};


var connection = new Connection(config);

function connect(request, response) {
  connection.on("connect", function(error) {
    //If no error, then good to go
    console.log("Connected to database! Booyah.");
    executeStatement();

    response.send("Connected to database! Booyah.");
  }, function (info) {
    console.log(info);
  });
}

exports.connect = connect;

我回应社区提供的答案.这是一个快速的代码示例,可以帮助您入门-

I echo the answers provided by the community. Here is a quick code sample that can help you get started -

var Connection = require('tedious').Connection;
var config = {
    userName: 'yourusername',
    password: 'yourpassword',
    server: 'yourserver.database.windows.net',
    // When you connect to Azure SQL Database, you need these next options.
    options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
    // If no error, then good to proceed.
    console.log("Connected");
    executeStatement();
    //executeStatement1();

});

var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

function executeStatement() {
    request = new Request("SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer;", function(err) {
    if (err) {
        console.log(err);} 
    });
    var result = "";
    request.on('row', function(columns) {
        columns.forEach(function(column) {
          if (column.value === null) {
            console.log('NULL');
          } else {
            result+= column.value + " ";
          }
        });
        console.log(result);
        result ="";
    });

    request.on('done', function(rowCount, more) {
    console.log(rowCount + ' rows returned');
    });
    connection.execSql(request);
}
function executeStatement1() {
    request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP);", function(err) {
     if (err) {
        console.log(err);} 
    });
    request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');
    request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');
    request.addParameter('Cost', TYPES.Int, 11);
    request.addParameter('Price', TYPES.Int,11);
    request.on('row', function(columns) {
        columns.forEach(function(column) {
          if (column.value === null) {
            console.log('NULL');
          } else {
            console.log("Product id of inserted item is " + column.value);
          }
        });
    });     
    connection.execSql(request);
}

关于防火墙规则,这取决于您在哪里运行应用程序.如果在Heroku上运行它,则必须添加Heroku服务器的IP.它是Linux VM吗?这是您可能想要的堆栈溢出 answer 签出.

About the firewall rule, it depends on where you are running the app. If you are running it on Heroku, you have to add the IP of the Heroku server. Is it a Linux VM? Here is a stack overflow answer that you might want to check out.