-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
108 lines (87 loc) · 3.03 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const mongoose = require("mongoose");
const express = require("express");
const path = require('path');
var cors = require("cors");
const bodyParser = require("body-parser");
const logger = require("morgan");
const Data = require("./data");
const app = express();
app.use(cors());
const router = express.Router();
// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));
// This is the MongoDB database
const dbRoute = "mongodb+srv://bjs250:DeltaV123@cluster0-rhktc.mongodb.net/DrugInfoDB?retryWrites=true";
// Connects back end code with the database
mongoose.connect(process.env.PROD_MONGODB ||
dbRoute,
{ useNewUrlParser: true }
);
let db = mongoose.connection;
db.once("open", () => console.log("connected to the database"));
// Checks if connection with the database is successful
db.on("error", console.error.bind(console, "MongoDB connection error:"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger("dev"));
/* Main Code starts here */
router.get("/getDrugNames", (req, res) => {
Data.distinct("name", (err, data) => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
});
});
router.get("/getDeliveryMethods/:drugName", (req, res) => {
const { drugName } = req.params;
Data.distinct("delivery", { name: drugName }, (err, data) => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
});
});
router.get("/getDosageAmounts/", (req, res) => {
const { drugName, deliveryMethod } = req.query;
Data.distinct("dosage", { name: drugName, delivery: deliveryMethod }, (err, data) => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
});
});
router.get("/getData/", (req, res) => {
const { drugName, deliveryMethod, dosage } = req.query;
Data.aggregate([
{
$match:
{
name: drugName,
delivery: deliveryMethod,
dosage: dosage
}
},
{
$group:
{
_id: "$hospital",
mean_price: { $avg: "$price" },
total_records: { $sum: 1 },
values: { $push: "$price" },
max_price: { $max: "$price" },
min_price: { $min: "$price" },
standard_deviation: { $stdDevSamp: "$price" },
description: {$first : "$description"}
}
}
]).sort({mean_price:1}).exec(function (err, data) {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });
});
});
// Append /api for http requests
app.use("/api", router);
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
// Launch backend into a port
app.listen(process.env.PORT || 3001, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});