0

I have developed a rest api in nodejs. My database is in Azure SQL.

I have used SWORM npm package to connect to SQL Server.

Following is my code to connect to the Azure SQL database.

var conn = config.get('connectionString');
        var db = sworm.db({
            driver: 'mssql',
            config: {
                user: conn.get('user'),
                password: conn.get('password'),
                host: conn.get('host'),
                database: conn.get('database')
            }
        });

When I execute this code, I get following error

"message": "Failed to connect to undefined:1433 - connect ECONNREFUSED 127.0.0.1:1433", "code": "ESOCKET"

I don't know what's wrong

5
  • 1
    It looks like your code is trying to connect to a database server hosted locally (127.0.0.1). I would recommend checking the connection string to make sure it is correct. Commented Oct 23, 2016 at 6:17
  • Yes. Why so? My connection string in config is fine, pointing to Azure sql Commented Oct 23, 2016 at 6:19
  • Do I need to make any changes to driver. Currently, I use 'mssql'. Commented Oct 23, 2016 at 6:24
  • 1
    @MARKANDBhatt - I think you missed the point Gaurav was making: Did you confirm that your connection string is correct after reading it in from the config file? That is: Did you check the value of host? Commented Oct 23, 2016 at 12:30
  • Yes. The connectionstring is correct. nothing wrong with it. I can connect to Azure sql database using visual studio. Commented Oct 23, 2016 at 12:44

1 Answer 1

2

Please double check the variable value of conn, per your error message, it was truly accessing your local MsSQL server. Please try the following code snippet for sworm connecting to Azure SQL Server:

var sworm = require('sworm');

var db = sworm.db();

db.connect({
  driver: 'mssql',
  config: {
    user: '<user>@<server>',
    password: '<password>',
    server: '<server>.database.windows.net',
    database: '<database>',
    options: {
        encrypt: true // Use this if you're on Windows Azure
    }
  }
}).then(()=>{
    db.query('select 1 as number').then((results)=>{
        console.log(results);
    })
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Gary. I tried this code to connect my local machine with Azure SQL. I got timeout exception. I have added my ip in Azure SQL's firewall exception.
the options: { encrypt: true } solved for me. Thaaaanks a lot sir.

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.