Fernando Delgadillo
Fernando Delgadillo

Reputation: 25

Connection with rest-API and SQL server or Azure

I have a problem with the connection to my database located in Azure, I was attempting to do a connection with a rest-API that I create, to a database that I have in Azure, this database I manage directly from SQL Server, and I can't make a connection with this.

I attempt to connect with another test database in SQL Server.

The rest-API ia create is in NodeJS

var sql = require('mssql');
var dbconfig = {
    server:"Fernando\EQUIPO",
    user: "<user>",
    password: "<password>",
    database: "<dbname>",
    port: 1433,
    option: {
        encrypt: false
    }
};

function getList() {
    var record;
    var conn = new sql.ConnectionPool(dbconfig);
    conn.connect(function(err){
        if(err) throw err;
        var req = new sql.Request(conn);
        req.query("select * from cliente", function(err, recordset) {
            if(err) throw err;
            else {
                console.log(recordset);
                record = recordset;
            }
            conn.close();
        });
    });
    return record;
}

const { Router } = require('express');
const router = Router();

const _ = require('underscore');

const movies = require('../sample.json');

router.get('/', (req, res) => {
    res.send(getList());
});

When I make a "get" to my local host http://localhost:3000/api/movies appears the following message in the console:

GET /api/movies 200 126.188 ms - -
(node:11868) UnhandledPromiseRejectionWarning: ConnectionError: Failed to connect to FernandoEQUIPO:1433 - getaddrinfo ENOTFOUND FernandoEQUIPO
    at Connection.tedious.once.err (C:\Users\luisn\Desktop\rest-API\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at Connection.socketError (C:\Users\luisn\Desktop\rest-API\node_modules\tedious\lib\connection.js:1258:12)
    at _connector.Connector.execute (C:\Users\luisn\Desktop\rest-API\node_modules\tedious\lib\connection.js:1084:21)
    at GetAddrInfoReqWrap._dns.default.lookup [as callback] (C:\Users\luisn\Desktop\rest-API\node_modules\tedious\lib\connector.js:152:16)        
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:68:17)
(node:11868) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11868) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Upvotes: 0

Views: 1165

Answers (2)

Leon Yue
Leon Yue

Reputation: 16401

Connect to Azure SQL database:

Here's the example code connect to Azure SQL database with REST-API: enter image description here

Note: the encrypt must be true.

You can get the server name from on Portal: enter image description here

And you need to add the client IP address to the Azure SQL Server firewall settings: enter image description here

Then you could connect to the Azure SQL database now. You can reference:Creating a Node.js REST API in Azure

Connect to SQL Server:

Your code is almost correct and need some change:

var sql = require('mssql');
var dbconfig = {
    server:"localhost",
    user: "<user>",
    password: "<password>",
    database: "<dbname>",
    port: 1433,
    option: {
        encrypt: false
    }
};

Like Abhishek Ranjan said, you should enter your server ip.

Hope this helps.

Upvotes: 1

Abhishek Ranjan
Abhishek Ranjan

Reputation: 497

Welcome to StackOverflow.

ConnectionError: Failed to connect to FernandoEQUIPO:1433 - getaddrinfo ENOTFOUND FernandoEQUIPO

Your error states that it is unable to connect as it could not find a server at the given address.Make sure there is a server runninng and verify connection using some third party app.
Are you sure FernandoEQUIPO gets resolved to a proper hostname ?

Upvotes: 0

Related Questions