TheBasementNerd
TheBasementNerd

Reputation: 83

I cannot connect to SQL server using Node.JS server

module.exports = {
    "user": 'ThisUsername',
    "password": 'ThisPassword',
    "server": '169.254.4.58',
    "database": 'ThisDatabase',
    "pool": {
        "max": 10,
        "min": 0,
        "idleTimeoutMillis": 30000
    }
};

This is the config object, stored in a separate file so the values aren't hardcoded in

let sql = require('mssql');
var connStr = require('./connStr.js');

const connectionString = process.env.CONNECTION_STRING||'';
//console.log(process.env);
console.log(connectionString);

sql.on('error', err => {
  // ToDo: For testing only -- remove console.log later
  console.log(err);
});

// addapted from https://stackoverflow.com/questions/30356148/how-can-i-use-a-single-mssql-connection-pool-across-several-routes-in-an-express
const poolPromise = sql.connect(connStr)
  .then(pool => {
    console.log('SQL connection established.');
    return pool;
  })
  .catch(err => SqlClose(err));

module.exports = { sql, poolPromise };

function SqlClose(thisErr) {
    console.log(`SQL connection failed - \n${thisErr}`);
    sql.close();
};

This is the code that connects to the SQL server

C:\ThisDirectory>npm run dev

> [email protected] dev C:\ThisDirectory
> env-cmd -f ./config/dev.env node ./src/index.js


tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23
Server is up on port 3000
SQL connection failed -
ConnectionError: Failed to connect to 169.254.4.58:1433 in 15000ms

This is the error on attempting to connect

Server is up on port 3000 refers to the Node.JS server SQL connection failed refers to the SQL server connection

It would appear that one of these files is the reason I cannot connect. I am unable to enable TCP/IP on the SQL server due to restrictions on my work network, SQL Server Browser is already running, those are the only two things I can find online that help this problem apparently. I did not write this code but the dev has another important project to attend to. I am at a loss and I am over the deadline. Please help

Upvotes: 0

Views: 2990

Answers (1)

franvergara66
franvergara66

Reputation: 10784

You need to update your config as follow:

module.exports = {
    "user": 'ThisUsername',
    "password": 'ThisPassword',
    "server": '169.254.4.58',
    "database": 'ThisDatabase',
    "pool": {
        "max": 10,
        "min": 0,
        "idleTimeoutMillis": 30000
    },
    "options": {
    "encrypt": true,
    "enableArithAbort": true
    }
};

enableArithAbort must be passed in the options property.

Upvotes: 1

Related Questions