How to fetch data from MsSQL in NodeJS

Here , We connect MsSQL database using NodeJS and ExpressJS as server.

So, let’s start the article for How to fetch Data from MsSQL in NodeJS.

Before starting we need to two as pre-requriments. NodeJS and MsSQL database server access. Like HostName, Username, Password, ServerIP etc.

How to fetch data from MsSQL in NodeJS

Here is the code for package.json. ( https://github.com/prashant1879/NodeJS-connection-with-mssql )

{
    "name": "nodejs-connection-with-mssql",
    "version": "1.0.0",
    "description": "NodeJS connection with MsSQL for beginners. ",
    "main": "app.js",
    "scripts": {
        "test": "node app.js"
    },
    "keywords": [
        "nodejs-connection-with-mssql"
    ],
    "author": "Prashant Suthar",
    "license": "ISC",
    "dependencies": {
        "express": "4.17.1",
        "mssql": "6.1.0"
    }
}

app.js/server.js whatever you suite you can use.
use npm installl for installing node_module from package.json file.

here you have to change database host, username, password and database name.
I had create executeQuery function for execute query.
you can put this function in your helper or any common controller.

 

//Init NodeModules and Express server.
var express = require('express');
var app = module.exports = express();
var sql = require('mssql');

const PORT = 5555;

const config = {
    user: 'USERNAME', //Update me
    password: 'PASSWORD', //Update me
    server: 'LOCALHOST', // You can use 'localhost\\instance' to connect to named instance //Update me
    database: 'DATABASE', //Update me
}

//Setting up the server.
app.listen(PORT, function(){
	console.log('Server running at port '+ PORT +': http://127.0.0.1:' + PORT);
})

//Function to connect to database and execute query 
// You can put this on Global Helper or common function.
var executeQuery = function (res, query) {
    sql.connect(dbConfig, function (err) {
        if (err) {
            console.log("Error while connecting database :- " + err);
            res.send(err);
        } else {
            // create Request object
            var request = new sql.Request();
            // query to the database
            request.query(query, function (err, res) {
                if (err) {
                    console.log("Error while querying database :- " + err);
                    res.send(err);
                } else {
                    res.send(res);
                }
            });
        }
    });
}

//get request
app.get("/", function(req , res){
    var query = "SELECT * FROM [TABLE_NAME]";
    executeQuery (res, query);
});

I hope you like this article. Keep visiting my website for more upcoming articles. If you need any help with How to fetch data from MsSQL in NodeJS you can contact me. You can ask me questions in comments also. You can connect me on social media as well links are below in the footer section. Keep connected. Happy Coding.

Prashant Sutharhttps://prashantsuthar.com
My self Prashant Suthar, I had experience worked with NodeJS, Core PHP, WordPress, CodeIgniter, Shopify, Prestashop, Opencart and many frameworks. I had some basic knowledge about server setup and maintenance. I also worked with third parties APIs like Twillio audio,video, SMS API, FB messenger API and many more. I am working as team lead & sr. developer in Ahmedabad, GJ, IN.

Comments

Similar Articles