I can't connect to SQL Server with mssql library for my express project. I can connect with VS Code and SQL Server Management Studio
var express = require('express');
var app =
express();
const sql = require('mssql')
var config = {
user: 'eren',
password: 'PAssPoxx1.',
server: 'localhost',
database: 'AIRPLANE',
options: {
encrypt: true,
enableArithAbort: 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))
app.get('/', function (req, res) {
// Configuration object for your database
// connect to the database
sql.connect(config, function (err) {
console.log("x")
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 User', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(3000, function () {
console.log('Server is running..');
});
My errors says:
Database Connection Failed! Bad Config: ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence) at Connection. (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\lib\tedious\connection-pool.js:68:17) at Object.onceWrapper (events.js:422:26) at Connection.emit (events.js:315:20) at Connection.socketError (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:12)
at C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21 at SequentialConnectionStrategy.connect (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connector.js:87:14) at Socket.onError (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connector.js:100:12) at Socket.emit (events.js:315:20) at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) { code: 'ESOCKET', originalError: ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence) at ConnectionError (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\errors.js:13:12) at Connection.socketError (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:56)
at C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21 at SequentialConnectionStrategy.connect (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connector.js:87:14) at Socket.onError (C:\Users\Anıl Eren\Downloads\database-api\node_modules\mssql\node_modules\tedious\lib\connector.js:100:12) at Socket.emit (events.js:315:20) at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'ESOCKET' } }
EDIT: TCP/IP enabled Port 1433