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.