Search code examples
javascriptnode.jsangularsql-server-2014angular2-forms

Connect Nodejs and Angular 2 to SQL Server 2014


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 node test.js the code deletes the record properly.

Here is the 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');

After that I moved the same file to the src folder in my Angular 2 project (where the index.html file also exists). I put the same code into a function in test.js file like that:

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');



}

Now I want to call testConnection() from the index page. I have put the <script src="C:\Users\amandeep.singh\Desktop\Angular\my-app\src\test.js"> to script path and call the function using this:

<script>
testConnection();
</script>

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.

Help will be appreciated.


Solution

  • You can't run Node applications in the browser. Nodejs is an application that runs Javascript in Google's V8 Javascript VM on your operating system and is meant to be a backend system (at least in the web development stack).

    So you basically have to run your Node program on a webserver and make your API requests from the Angular application.

    There are several tutorials out there on the internet that help you with this.

    Here is the official Angular documentation angular.io