Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Promise.all([
- //the number of requests varied
- fetch('http://localhost:8080/api/report/favorite/check/1/1').then(value => value.json()),
- fetch('http://localhost:8080/api/report/favorite/check/1/4').then(value => value.json())
- ])
- .then((value) => {
- //I always get EALREADYCONNECTING error here from 2nd request and on
- console.log(value)
- })
- .catch((err) => {
- console.log(err);
- });
- //Initializing node modules
- var express = require("express");
- var bodyParser = require("body-parser");
- var sql = require("mssql");
- var app = express();
- // Body Parser Middleware
- app.use(bodyParser.json());
- //CORS Middleware
- app.use(function (req, res, next) {
- //Enabling CORS
- 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();
- });
- //Setting up server
- var server = app.listen(process.env.PORT || 8080, function () {
- var port = server.address().port;
- console.log("App now running on port", port);
- });
- var connPool = new sql.ConnectionPool({
- user: 'someuser',
- password: 'somepassword',
- server: 'some\thing',
- database: 'somedb',
- port: 12345
- });
- //Function to connect to database and execute query
- var executeQuery = function(res, query){
- connPool.connect().then(function () {
- // create request object
- var request = new sql.Request(connPool);
- // query to the database
- request.query(query).then(function (recordset) {
- res.send(recordset)
- connPool.close();
- })
- .catch(function (err) {
- console.log("Error while querying database :- " + err);
- res.send(err)
- connPool.close();
- });
- })
- .catch(function (err) {
- console.log("Error while connecting database :- " + err);
- res.send(err);
- });
- }
- //GET API
- app.get("/api/report/favorite/check/:userid/:reportid", function(req, res){
- var query = "EXEC rpt.proc_ReportFavoriteCheck " + req.params.userid + ", " + req.params.reportid;
- executeQuery (res, query);
- });
Add Comment
Please, Sign In to add comment