title: Connect Nodejs and Angular 2 to SQL Server 2014
body: <p>I am using Angular 2 and Nodejs to Connect to an SQL Server. If I simply put following code in a js file and run it through the console using <code>node test.js</code> the code deletes the record properly. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>var webconfig = {
      user: 'sa',
      password: 'test',
      server: 'localhost', 
      database: 'Test',
  
      options: {
          encrypt: false // Use this if you're on Windows Azure 
      }
  }
  
  var express = require('express');
  var sql = require('mssql');
  var http = require('http');
  
  var app = express();
  var port = process.env.PORT || 4200;
  
  var connection = new sql.Connection(webconfig, function(err) {
      var request = new sql.Request(connection); 
      request.query('delete from Employee where Id = 2382', function(err, recordset) {
         if(err)      // ... error checks 
              console.log('Database connection error');
  
      console.dir("User Data: "+recordset);
      });
  });
  
  
  app.listen(port);
  console.log(port+' is the magic port');
  </code></pre>
  
  <p>After that I moved the same file to the <code>src</code> folder in my Angular 2 project (where the index.html file also exists). I put the same code into a function in <code>test.js</code> file like that:</p>
  
  <pre><code>function testConnection()
  {
  var webconfig = {
            user: 'sa',
      password: 'test',
      server: 'localhost', 
      database: 'Test',
  
      options: {
          encrypt: false // Use this if you're on Windows Azure 
      }
  }
  
  var express = require('express');
  var sql = require('mssql');
  var http = require('http');
  
  var app = express();
  var port = process.env.PORT || 4200;
  
  var connection = new sql.Connection(webconfig, function(err) {
      var request = new sql.Request(connection); 
      request.query('delete from Employee where Id = 2382', function(err, recordset) {
         if(err)      // ... error checks 
              console.log('Database connection error');
  
      console.dir("User Data: "+recordset);
      });
  });
  
  
  app.listen(port);
  console.log(port+' is the magic port');
  
  
  
  }
  </code></pre>
  
  <p>Now I want to call <code>testConnection()</code> from the index page. I have put the <code>&lt;script src="C:\Users\amandeep.singh\Desktop\Angular\my-app\src\test.js"&gt;</code> to script path and call the function using this:</p>
  
  <pre><code>&lt;script&gt;
  testConnection();
  &lt;/script&gt;
  </code></pre>
  
  <p>The index page executes properly but doesn't show any error nor executes the command. I'm unable to understand why the same code works in the console on Nodejs but not in my index.html.</p>
  
  <p>Help will be appreciated.</p>