title: Querying database through hybrid connection from an azure node js backend
body: <p>I need to query an on-premises SQL Server Express database from my Azure app service based on Nodejs. I followed <a href="https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-hybrid-connection-connect-on-premises-sql-server" rel="nofollow noreferrer">this tutorial</a> to add a hybrid connection.</p>
  
  <p>I successfully connected and added this connection to my service. I also added the connection string as:</p>
  
  <pre><code>Server=LOCALHOST\\SQLEXPRESS,1433;Database=smartpointmovil_db;User ID=sa;Password=pass1009
  </code></pre>
  
  <p>Then, I wrote an easy api in node JS for my azure mobile app service:</p>
  
  <pre><code>"get": function (req, res, next) {
       var sql = require("mssql");
  
  var config = {
      server: 'LOCALHOST\\SQLEXPRESS',
      user: 'sa',
      password: 'pass1009',       
      database: 'smartpointmovil_db',
      port: 1433
  };
  
  
  var conn = new sql.Connection(config);
  var req=new sql.Request(conn);
  
    conn.connect(function(err){
        if(err){
             console.log("Error connectig: "+err);
             return;
        }
  
  
        req.query('select * from smartpointmovil.cat_cadenas where id=1',function(err,results){
                      if(err){
                             console.log("Error during query: "+err);
                             return;
                             }
                      else{
                            console.log("Success: "+results[0].cadena);
                            res.json(results[0]);
                           }
                      conn.close();
        });
  
    });
  
  }
  </code></pre>
  
  <p>Every time I call this API, I get the following error message:</p>
  
  <blockquote>
    <p>Error connectig: ConnectionError: Failed to connect to
    LOCALHOST:undefined in 15000ms</p>
  </blockquote>
  
  <p>I did not find the way to define in my code the connection string to be used for the query, so I am including the configuration parameters.</p>
  
  <p>Any idea how to write a node js code to query my local database from an Azure hosted service?</p>