How to connect MSSQL with Node.js

Programming, error messages and sample code > Node.js
The tedious module is a JavaScript implementation of the TDS protocol, which is supported by all modern versions of SQL Server. The driver is an open-source project, available on GitHub https://github.com/tediousjs/tedious.
 
You have to install tedious to your local node.js application manually, try the commands below.
>cd NodeAppFolder
>npm install tedious
 
Sample code:
var Connection = require('tedious').Connection;  
var Request = require('tedious').Request;  
var config = {  
	server: 'hostname',  //db host
	authentication: {
		type: 'default',
		options: {
			userName: 'username', //db user
			password: 'password' //db password
		}
	},
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
	if (err) throw err;
	console.log("Connected");  
	executeSQL();//execute a SQL query
});

connection.connect();
	
function executeSQL(){
	var request = new Request("select * from [table];", function(err) {
	if (err) { 
		console.log(err);}
	}); 
	var result = "";  
	request.on('row', function(columns) {  
		columns.forEach(function(column) {  
		  if (column.value === null) {  
			console.log('NULL');  
		  } else {  
			result += column.value + " ";  
		  }  
		});  
		console.log(result);  
		result ="";  
	});  
	// Close the connection
	request.on("requestCompleted", function (err) {
		if (err) throw err;
		connection.close();
	});
	connection.execSql(request); 
}