3

I have a database that I can connect to from ASP.NET. I'm now trying to connect to that database via Node.js. I'm trying to do some benchmarking. In an attempt to connect to the database via Node.js, I'm using the mssql package. My code looks like the following:

var sql = require('mssql');
var connectionString = {
    user: '[myUserName]',
    password: '[myPassword]',
    server: 'tcp:[serverName].database.windows.net',
    database: '[databaseName]',

    options: {
        encrypt: true // Use this if you're on Windows Azure
    }
};  

try {
  sql.connect(connectionString, function(err) {
    if (err) {
      console.log(err);
      return;
    }

    var request = new sql.Request();
    request.query('select top 5 from Logs', function(err, recordset) {
      if (err) {
        console.log(err);
        return;
      }
    });
  });
} catch (ex1) {

}

When I execute this code, I get an error that says:

{ name: 'ConnectionError',
  message: 'Failed to connect to tcp:[serverName].database.windows.net:1433 - getaddrinfo ENOTFOUND',
  code: 'ESOCKET' }

My question is, how do I connect to SQL Azure from Node.js? I'm trying to run a basic query. Unfortunately, I feel stuck. I have to be doing something improperly in my connection string. Any help is much appreciated.

5
  • I am facing the same problem in one of my two applications I have deployed on Azure. The weird thing is both are working fine locally but when I push them only one works, although they both have the same configurations/ query code. Commented Aug 26, 2015 at 8:16
  • Did you ever find a solution to this? Commented Dec 29, 2015 at 2:25
  • Did you setup the firewall and router when setting up the Azure database? the following post has some instructions that might help - azure.microsoft.com/en-us/documentation/articles/… Commented Dec 29, 2015 at 16:43
  • No I can connect to the database when running it anywhere else. Also I am even able to run a sample node script with in the command line utility at <myapp>.scm.azurewebsites.net and it connects fine to the database. It is only when it is running the server.js script which uses the exact code from my test script. Commented Dec 30, 2015 at 3:18
  • In your comment to a (now deleted) answer you mentioned that you found a solution to this problem. Please add your answer so that others can benefit from that knowledge. After adding the answer you can also wait 48 hours and then select your answer as the correct answer if no one else has provided a better answer. Commented Dec 30, 2015 at 16:21

1 Answer 1

1
+50

It very much depends on your node version, I had the same issue I downloaded the package msnodesqlv8, and it worked just fine after days of scavenging the web. The syntax is a bit different but the page has extremely good examples.

https://www.npmjs.com/package/msnodesqlv8

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.