title: React-Node.js app can't connect to SQL Server database
body: <p>This is my first time attempting this type of tech stack and I am not sure what I am missing to get my database to connect. When I attempt to go to <code>/api/broker</code>, it just loads for a long time... also, I never get the message that server is working from my / route.</p>
  
  <p>When I stop the loading of the <code>/api/broker</code> route, I get this message:</p>
  
  <blockquote>
    <p>Proxy Error: See <a href="https://nodejs.org/api/errors.html#errors_common_system_errors" rel="nofollow noreferrer">https://nodejs.org/api/errors.html#errors_common_system_errors</a> for more information (ECONNRESET).</p>
  </blockquote>
  
  <p>How would I get the SQL Server to accept the connection? Any idea what I am missing or doing wrong?</p>
  
  <p>This is my app.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import React, { Component } from 'react';
  import './App.css';
  import Header from './components/header';
  // import './server';
  
  
  class App extends Component {
    state = {
        data: null
      };
    
      componentDidMount() {
          // Call our fetch function below once the component mounts
        this.callBackendAPI()
          .then(res =&gt; this.setState({ data: res.express }))
          .catch(err =&gt; console.log(err));
      }
        // Fetches our GET route from the Express server. (Note the route we are fetching matches the GET route from server.js
      callBackendAPI = async () =&gt; {
        const response = await fetch('/api/broker');
        const body = await response.json();
    
        if (response.status !== 200) {
          throw Error(body.message) 
        }
        return body;
      };
  
      render() {
      return (
        &lt;div className="App"&gt;
          &lt;Header /&gt;
            &lt;p&gt;
              Edit &lt;code&gt;src/App.js&lt;/code&gt; and save to reload.
            &lt;/p&gt;
        
        &lt;/div&gt;
      );
  }
  }
  
  export default App;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And this is my server.js file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>const express = require("express");
  const bodyParser = require ("body-parser");
  const sql = require('mssql');
  const app = express();
  
  app.use(express.static('public'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));
  
  app.use(function(res, req, next){
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType, Content-Type, Accept, Authorization");
      next();
  });
  
  var dbConfig = {
      user: "user",
      password: "password",
      server: "jdbc:sqlserver://sql3\\misc",
      database: "ReactApp_DB"
  };
  
  //Function to connect to database and execute query
  var  executeQuery = function(res, query){             
      sql.connect(dbConfig, function (err) {
          if (err) {   
                      console.log("Error while connecting database :- " + err);
                      res.send(err);
                   }
                   else {
                          // create Request object
                          var request = new sql.Request();
                          // query to the database
                          request.query(query, function (err, res) {
                            if (err) {
                                       console.log("Error while querying database :- " + err);
                                       res.send(err);
                                      }
                                      else {
                                        res.send(res);
                                        console.log("Querying database ");
                                             }
                                });
                        }
       });           
  }
  
  app.get('/', function(req, res) {
   res.send('Server is working...');
  });
  
  //GET API
  app.get("/api/broker", function(req , res){
      console.log('brokers queried: ');
      var query = "select * from [broker]";
      executeQuery (res, query);
  });
  
  var server = app.listen(3000, function (){
      var port = server.address().port;
      console.log("app now running on port ", port);
  });</code></pre>
  </div>
  </div>
  </p>