Node.js SQL server access

In this tutorial, we will learn how to access the SQL server in the node.js application using express.js.

We can access the SQL server in the node.js application but first need to install the SQL server driver that can be downloaded here Download


Based on the node-sqlserver, this version is compiled in the Visual Studio 2015, 2017 and is built against the v8 node module.


SQL Driver installation


We can install mssql driver using NPM command like shown below

npm install mssql

After installation, we can connect to SQL server and can use in the node application. For example, we have below SQL table "employee" in the SQL server


ID Name City Country
1 Smith Chicago USA
2 Adam New York USA
3 Andrew Los Angeles USA
4 Filtoff San Antonio USA

Once apply the above command it will display result as shown below

var express = require("express");
var appli = express();

appli.get('/', function (req, res) {

var varsql = require("mssql");

// config database here
var config = {
user: 'user',
password: 'password',
server: 'databaseserver',
database: 'dbname'
// connect to database here
sql.connect(config, function (err) {

if (err) console.log(err);

// create request for object
var request = new sql.Request();

// SQL query to fetch rcords
request.query('Select id, name, city, country  from employee', function (err, recordset) {

if (err) console.log(err)

// send records with recordset


var server = appli .listen(5000, function () {
console.log('Running Server');


[{"id" : 1, "name" : "Smith" : "Chicago"}]
[{"id" : 2, "Adam" : "New York" : "USA"}]
[{"id" : 3, "Andrew" : "New York" : "USA"}]
[{"id" : 4, "Filtoff" : "San Antonio" : "USA"}]