Accessing the Microsoft SQL server in Node.js
Node.js provides different database drivers that can be added as node packages. There are packages for the MongoDB driver, the Microsoft SQL Server driver, and more. We will use the MS SQL driver for Node.js to connect with Microsoft SQL server databases. To install mssql
you can run the npm install mssql –save
command, or add it from the NPM package manager window, as shown in the following screenshot:
Tip
With the MSSQL driver, TCP/IP should be enabled for a corresponding SQL server instance.
Reading a record from the Microsoft SQL server database
In the DataService.js
file, we will add the getProducts()
method, which loads the list of products from the SQL Server database.
The following is the getProducts()
method, which accepts the callback function, so as soon the products list is fetched from the database, it will be passed in the callback function on the caller side:
(function(data){ data.getRecords = function (callbackFn) { //loaded...