I have developed node js Rest API program where get request call by different location. but when GetSales request call first time it is working fine, but if we again call GetSales request when GetSales already running then my API Get Fails. I want GetSales should work when multiple user calls Get Method function at same time.

enter code here
var easyxml = require('easyxml');
var _sqlPackage = require("mssql");  
var _expressPackage = require("express");  
var _bodyParserPackage = require("body-parser");  
var http = require('http');
var jsonxml = require('jsontoxml');
var app = _expressPackage();  

const auth = require('./auth.js');
app.use(auth);


app.use(function (req, res, next) {  
res.header("Access-Control-Allow-Origin", "*");  
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");  
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,contentType,Content-Type, Accept, Authorization");  
next();  
});  


var server = app.listen(process.env.PORT || 3000, function () {  
var port = server.address().port;  
console.log("App now running on port", port);  
});  


var dbConfig = {  
user: "SA",  
password: "[email protected]",  
server: "10.1.101.12\\PC_ADMIN",
database: "SalesDB",
connectionTimeout: 1200000,
requestTimeout: 1200000
};  



var SqlQueryExecuteFunction =  function (response, strQuery) {  
    _sqlPackage.close();      
    _sqlPackage.connect(dbConfig, function (error) {  
    if (error) {  
    console.log("Error while connecting to database :- " + error);  
    response.send(error);  
    }  
    else {  
    var request = new _sqlPackage.Request();  
    request.query(strQuery, function (error, responseResult) {  
        if (error) {  
        console.log("Error while connecting to database:- " + error);  
        }  
        else {  
        var xml = jsonxml(responseResult);
        response.send(xml);
        }  
    });  
    }  
});
}


app.get("/getSales/:DealerCode/:DateInsert", function(_req ,_res) {
var _DealerCode = _req.params.DealerCode;
var _DateInsert = _req.params.DateInsert;
var Sqlquery = "SELECT * FROM Sales where DealerCode  ='"+ _DealerCode+"'  and DateInsert >'"+ _DateInsert+"' for xml path ('Transaction'), root('SalesMaster')" ;
SqlQueryExecuteFunction(_res, Sqlquery);
});

0 Answers