Guest User

Untitled

a guest
Jan 20th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. Promise.all([
  2. //the number of requests varied
  3. fetch('http://localhost:8080/api/report/favorite/check/1/1').then(value => value.json()),
  4. fetch('http://localhost:8080/api/report/favorite/check/1/4').then(value => value.json())
  5. ])
  6. .then((value) => {
  7. //I always get EALREADYCONNECTING error here from 2nd request and on
  8. console.log(value)
  9. })
  10. .catch((err) => {
  11. console.log(err);
  12. });
  13.  
  14. //Initializing node modules
  15. var express = require("express");
  16. var bodyParser = require("body-parser");
  17. var sql = require("mssql");
  18. var app = express();
  19.  
  20. // Body Parser Middleware
  21. app.use(bodyParser.json());
  22.  
  23. //CORS Middleware
  24. app.use(function (req, res, next) {
  25. //Enabling CORS
  26. res.header("Access-Control-Allow-Origin", "*");
  27. res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
  28. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
  29. next();
  30. });
  31.  
  32. //Setting up server
  33. var server = app.listen(process.env.PORT || 8080, function () {
  34. var port = server.address().port;
  35. console.log("App now running on port", port);
  36. });
  37.  
  38. var connPool = new sql.ConnectionPool({
  39. user: 'someuser',
  40. password: 'somepassword',
  41. server: 'some\thing',
  42. database: 'somedb',
  43. port: 12345
  44. });
  45.  
  46. //Function to connect to database and execute query
  47. var executeQuery = function(res, query){
  48. connPool.connect().then(function () {
  49. // create request object
  50. var request = new sql.Request(connPool);
  51. // query to the database
  52. request.query(query).then(function (recordset) {
  53. res.send(recordset)
  54. connPool.close();
  55. })
  56. .catch(function (err) {
  57. console.log("Error while querying database :- " + err);
  58. res.send(err)
  59. connPool.close();
  60. });
  61. })
  62. .catch(function (err) {
  63. console.log("Error while connecting database :- " + err);
  64. res.send(err);
  65. });
  66. }
  67.  
  68. //GET API
  69. app.get("/api/report/favorite/check/:userid/:reportid", function(req, res){
  70. var query = "EXEC rpt.proc_ReportFavoriteCheck " + req.params.userid + ", " + req.params.reportid;
  71. executeQuery (res, query);
  72. });
Add Comment
Please, Sign In to add comment