在第一次调用完成之前多次获取请求调用时,节点js rest api失败

时间:2019-05-12 15:59:21

标签: node.js rest api

我开发了node js Rest API程序,该程序在不同位置获取请求调用。但是,当第一次第一次调用GetSales时,它运行良好,但是如果我们在GetSales已经运行时再次调用GetSales请求,则我的API获取失败。我希望当多个用户同时调用Get Method函数时,GetSales应该能工作。

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: "Password@123",  
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 个答案:

没有答案