0

I'm trying to connect to my SQL Server database from a NodeJS n ExpressJS application. My target is to build a API that would do CRUD operations.

The error I am getting:

Database Connection Failed! Bad Config!: ConnectionError: Error: [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made

My nodeJS config file is as follows:

dbConfig.js:

const sql = require('mssql/msnodesqlv8')

const config = {
    database: 'ApiDemoDB',
    server: '.',
    driver: 'msnodesqlv8',
    options: {
      trustedConnection: true
    }

  }

  const poolPromise = new sql.ConnectionPool(config)
  .connect()
  .then(pool => {
    console.log('Connected to MSSQL')
    return pool
  })
  .catch(err => console.log('Database Connection Failed! Bad Config!:  ', err))

  module.exports = {
    sql, poolPromise
  }

Relevant block of code from my server.js file is as follows:

const express = require('express')

const bodyParser = require('body-parser')
const cors = require('cors')
const fs = require('fs')
const path = require('path')
const morgan = require('morgan')
const db=require('./db/dbConfig');

const app = express()

app.use(cors())

app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(morgan('dev'))

app.use(db.poolPromise, (req, res)=>
{
    console.log('Database connection successful with config! ');
    res.json('Database connection successful with config! ');
})

const port = 3500

app.listen(process.env.PORT || port , (err) => {
  if(err)
  {
    console.log('Unable to start the server!')
  }
  else
    console.log('NodeExpress Data API started running on : ' + port)
})

My database server has Windows authentication. No case of username and password. That's pretty much it at the mo. barebones. Can anyone help me resolve this trouble? Where are the errors in my code/changes I need to make in my code? Any special configuration required to set with my SQL Server installed instance?


Here's some more details about the error message I'm getting.

Here's some more details from my error message:

ConnectionError: Error: [Microsoft][SQL Server Native Client 11.0]TCP Provider: No such host is known.
,Error: [Microsoft][SQL Server Native Client 11.0]Login timeout expired,Error: [Microsoft][SQL Server Native Client
11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections.
jarlh
  • 42,561
  • 8
  • 45
  • 63
Prabir Choudhury
  • 143
  • 1
  • 18
  • 1
    Is TCP enabled as a protocol in the SQL Server Configuration Manager? Is the SQL Server the default instance or is it a named instance? (By default SQL Server Express uses a named instance called `SQLExpress`) If it's a named instance then the server name should be `.\YourInstanceName` and the SQL Browser service must be started. And if you are connecting from another machine then replace `.` with the server machine name, and open the correct firewall ports. – Charlieface Oct 23 '22 at 13:42
  • hi there @Charlieface in my case, it's a default named Instance. It is = T295D64\SQLEXPRESS. So I guess, I have to put in .\SQLEXPRESS. I checked configuration manager where the TCP/IP was disabled. I have enabled it now. But I cannot enable SQL Server Browser as that option is grayed out[disabled] under the SQL Server Services. What to do? – Prabir Choudhury Oct 23 '22 at 14:06
  • the poolPromise func. in the dbConfig where the code is failing.. – Prabir Choudhury Oct 23 '22 at 14:18
  • Default and named are mutually exclusive. You have a named instance called `SQLEXPRESS`, so yes your connection string should be `.\SQLEXPRESS` assuming you are working off the same machine. You might need to enable the Browser service first, before you start it. See https://stackoverflow.com/a/23283641/14868997 TCP is not necessary on the same machine. – Charlieface Oct 23 '22 at 14:24
  • yes, it's a default instance, so server name I changed to server: '.\SQLEXPRESS' in the dbconfig file. thanks for that link, @Charlieface I've started the SQL Server Browser Service. Now looking, trying again .... – Prabir Choudhury Oct 23 '22 at 14:33
  • nope. no luck ... the error is still exactly the same.... – Prabir Choudhury Oct 23 '22 at 14:36
  • 1
    If you change server protocol configuration in SQL Server (version) Configuration Manager you need to restart the instance for the changes to take effect. Did you also restart the SQLExpress instance? – AlwaysLearning Oct 23 '22 at 21:07
  • good point @AlwaysLearning did that and although my problem isn't solved the error message has changed. New error message is --> const poolPromise = new sql.ConnectionPool(config) ^TypeError: sql.ConnectionPool is not a constructor Also now I am doing const sql=require('msnodesqlv8') directly, instead of (mssql/msnodesqlv8) – Prabir Choudhury Oct 24 '22 at 13:19

0 Answers0