Node Version: 4.1.1
OS: Windows 10
var config = {
user: '...',
password: '......',
server: 'CITS-2F2T9R1\SQLEXPRESS', // localhost instance name
database: 'Info',
}
{ [ConnectionError: Failed to connect to CITS-2F2T9R1:undefined in 15000ms]
name: 'ConnectionError',
message: 'Failed to connect to CITS-2F2T9R1:undefined in 15000ms',
code: 'ETIMEOUT' }
It looks like the backslash is not getting escaped properly.
You have to escape the backslash yourself. Use 'CITS-2F2T9R1\\SQLEXPRESS'. That's because in Javascript the backslash is used to express characters like tab \t and linefeed \n.
I've the same issue, I defined my config as:
server: 'localhost\SQLEXPRESS'
but got the below error:
500: Internal Server Error :: ConnectionError: Port for SQLEXPRESS not found in ServerName;SCM;InstanceName;SQLEXPRESS;IsClustered;No;Version;12.0.2000.8;;
the "SCM" is my computer name.
thanks
In my case, when I use "NAME OF COMPUTER" works.
But when I use "NAME OF COMPUTER \ SQLEXPRESS", gives the error "ConnectionError: Failed to connect to NAMEOFCOMPUTER: undefined in 15000ms"
Any solutions for this ? I also cannot connect to sql express 2014. I tried every possible idea
Does your SQL Server have TCP protocol enabled? There's a tutorial how to do that.
Make sure SQL Server Browser service is not running. This is a windows service that clients can connect to and get the IP address of the instance they are looking for. As soon as I enabled this service, my connections started working.
Closing due to inactivity.
I have enabled the TCP Connections and Also the browser service is running still i am facing the same issue. Could have a look at the images i have attached and provide my with a solution. I have attched the error , sql server connection and TCP protocol snippets.
My server.js is as follows:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
//user: 'HP-SYS\Kishan',
//password: '',
server: 'HP-SYS\SQLEXPRESS',
database: 'FootballLeague'
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from Leagues', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
TCP Settings:

@Kishanvikani
Try adding another backslash within the server value 'HP-SYS\\SQLEXPRESS'
There should be two backslashes between HP-SYS and SQLEXPRESS but it appears GitHub may be eating one of them.
@designerx2
I guess there was some problem with user name now i have this thing coming up on my chrome browser:
Error: Global connection already exists. Call sql.close() first.
at Object.connect (D:\Work\Nodejs\node_modules\mssql\lib\base.js:1591:31)
at D:\Work\Nodejs\server.js:17:9
at Layer.handle [as handle_request] (D:\Work\Nodejs\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Work\Nodejs\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Work\Nodejs\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Work\Nodejs\node_modules\express\lib\router\layer.js:95:5)
at D:\Work\Nodejs\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Work\Nodejs\node_modules\express\lib\router\index.js:335:12)
at next (D:\Work\Nodejs\node_modules\express\lib\router\index.js:275:10)
at expressInit (D:\Work\Nodejs\node_modules\express\lib\middleware\init.js:40:5)
when no port is available for your server instance this error might occur,
check your TCP port number in sql server configuration manager as shown below:
sql server configuration manager port number
again check the port numbers , which the server is listening to , by following link below:
make sure both are same
I found my solution in this way. I used a local sql server instance and my config was
_var sql = require('mssql');
var config = {
server: 'myMachine\\SQLEXPRESS',
database: 'MyDB',
username :'XXXX',
password : 'XXXXXXXXX'
options: {
trustedConnection: true
},
port: 52087
};
function connection()
{
return new sql.ConnectionPool(config);
}_

{ user: 'sa',
password: '12345678',
server: 'DESKTOP-1OV0Q76\\SQLEXPRESS',
database: 'TestLocal',
options: { trustedConnection: true },
port: 49702 }
{ ConnectionError: Failed to connect to DESKTOP-1OV0Q76\SQLEXPRESS in 15000ms
at Connection.tedious.once.err (I:\OutSourcing\LP_Api\node_modules\mssql\lib\tedious.js:244:17)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.connectTimeout (I:\OutSourcing\LP_Api\node_modules\tedious\lib\connection.js:1027:10)
at Timeout.connectTimer.setTimeout [as _onTimeout] (I:\OutSourcing\LP_Api\node_modules\tedious\lib\connection.js:1001:12)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
code: 'ETIMEOUT',
originalError:
{ ConnectionError: Failed to connect to DESKTOP-1OV0Q76\SQLEXPRESS in 15000ms
at ConnectionError (I:\OutSourcing\LP_Api\node_modules\tedious\lib\errors.js:13:12)
at Connection.connectTimeout (I:\OutSourcing\LP_Api\node_modules\tedious\lib\connection.js:1027:26)
at Timeout.connectTimer.setTimeout [as _onTimeout] (I:\OutSourcing\LP_Api\node_modules\tedious\lib\connection.js:1001:12)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
message: 'Failed to connect to DESKTOP-1OV0Q76\\SQLEXPRESS in 15000ms',
code: 'ETIMEOUT' },
name: 'ConnectionError' } 'error'
I've try all solution but still showing same error.
Change server: 'DESKTOP-1OV0Q76\SQLEXPRESS' to server: 'DESKTOP-1OV0Q76\\SQLEXPRESS'. Note the extra back slash.
1OV0Q76
use this DESKTOP-1OV0Q76\\SQLEXPRESS but its not working again getting same error.
You can modify or deactivate the firewall or the antivirus, this is the final alternative.
Following process resolved my same issue
Write new query in mssql management studio
USE master
Go
xp_readerrorlog 0,1, N'Server is listening on'
Go
Execute this query. You will get Server listening on different port.
Use this port no in config to connect db as follows
server.js
var express=require('express');
var app=express();
var SQL=require('mssql');
app.get('/',function(req,res){
var config={
user:'test',
password:'db123',
server:'localhost',
database:'demodb'
options:{
trusted connection:true,
enableArithAbort:true
},
port:1434
}
SQL.connect(config,function(err){
if(err)
console.log(err):
var request=new sql.Request();
request.query('select * from tblstud', function (err,recorder){
if(err)
console.log('QueryError',err);
res.send(recordset);
});
});
});
var server=app.listen(5000,function(){
console.log('Server is running');
});
then run node server.js
& run in browser
http://localhost:5000
Most helpful comment
Make sure SQL Server Browser service is not running. This is a windows service that clients can connect to and get the IP address of the instance they are looking for. As soon as I enabled this service, my connections started working.