2

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.

2
  • A NodeJS JavaScript code cannot be directly executed in browser. NodeJS code is supposed to work as a backend. You need to write web-services in the NodeJS/Express code which exposes the backend logic. Then in the Angular code, web-service needs to be called via $http service. Commented Jul 19, 2017 at 7:51
  • Changed your tag from angularjs to angular since angular 2 isn't the same thing as angularjs. Commented Jul 19, 2017 at 8:07

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Wernerson, Now i have created API inside Node and using it in project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.