0

I'm trying to connect node js script to MsSQL database server, but fails.

My Microsoft SQL Server Management is however able to connect to the database.

node.js code

var Connection = require('tedious').Connection;  
var config = {  
    server: "172.168.200.35",
    dialect: "mssql",
    port:51433,
    authentication: {
        type: 'default',
        options: {
            userName: 'testuser',
            password: 'abc123'
        }
    },
    options: {
        database: 'IoTDb'
    }
};  
var connection = new Connection(config);  
// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
if (err) {
    console.error(err.message);
} else {
    executeStatement();
}
});

When running the code, I get this error

Failed to connect to 172.168.200.35:51433:1433 - getaddrinfo ENOTFOUND 172.168.200.35:51433

I don't understand why its fails to connect port 1433, as the port is defined as 51433

However, when I try to connect to the database via this login info (same as node config variable) enter image description here

It connects and I get this view over the database enter image description here

For info about the database, it is running via docker on another pc, but is reachable

I moved the port to options and now the error is this

Failed to connect to 172.168.200.35:51433:51433 - getaddrinfo ENOTFOUND 172.168.200.35:51433

7
  • SSMS is nothing more than a client application, just like your application. If one succeeds and the other fails, the problem is in the code. Commented Dec 9, 2021 at 11:42
  • I may be wrong, but shouldn't nodejs connect to 51433 not 1433 Commented Dec 9, 2021 at 11:43
  • In my config variable, I have defined the port as 51433 Commented Dec 9, 2021 at 11:44
  • The error says Failed to connect to 172.168.200.35:1433. That's not the same port. Are you sure you set the correct parameter? Commented Dec 9, 2021 at 11:45
  • I see. I added the port to the server in node and the new error can be seen Commented Dec 9, 2021 at 11:46

1 Answer 1

1

As this issue shows the port should be specified in options:

var config = {  
    server: "172.168.200.35",
    dialect: "mssql",
...
    options: {
        database: 'IoTDb',
        port:51433,
    }
};  
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.