-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
114 lines (79 loc) · 2.71 KB
/
app.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
109
110
111
112
113
114
// Getting environment variables
const dotenv = require("dotenv");
// Setting up server
const express = require('express')
const app = express()
//const port = process.env.PORT;
const Log = require('./models/Log')
// Configuration files s
let config = require('config'); //we load the db location from the JSON files
const dbHost = config.get('dbConfig.host')
const port = config.get('dbConfig.port')
dotenv.config()
//console.log("Rscript path:" + process.env.RSCRIPTPROCESSDATA)
// Connecting to DB
const mongoose = require('mongoose')
var connectWithRetry = function () {
console.log("Connecting to datase")
return mongoose.connect(dbHost, {
useNewUrlParser: true,
useUnifiedTopology: true
}, function (err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec \n ', err);
setTimeout(connectWithRetry, 5000);
}
});
}
connectWithRetry()
const db = mongoose.connection;
db.once("open", (_) => {
console.log("Database connected:", dbHost);
});
// db.on("error", (err) => {
// console.error("connection error:", err);
// });
// Ensuring that queries are not limited by size
app.use(express.json({ limit: '200mb' }));
app.use(express.urlencoded({ limit: '200mb', extended: true }));
// Defining routes
const dataRoute = require("./routes/data")
const metaDataRoute = require("./routes/metaData")
const processDataRoute = require("./routes/processData")
const deleteProjectRoute = require("./routes/deleteProject")
const projectDataRoute = require("./routes/projectData")
const unitsAndConversionsRoute = require("./routes/unitsAndConversions")
app.use("/api/data/", dataRoute)
app.use("/api/meta-data", metaDataRoute)
app.use("/api/process-data", processDataRoute)
app.use("/api/delete-project", deleteProjectRoute)
app.use("/api/project-data", projectDataRoute)
app.use("/api/conversions", unitsAndConversionsRoute)
// Defining a get request for the home page
app.get('/', (req, res) => {
res.send('Welcome to the RHoMIS API application')
})
app.get('/logs', async (req, res) => {
let header = `
<!DOCTYPE html>
<html>
<body>
<pre id="json"></pre>
<script>
var data =
`
let footer = `
document.getElementById("json").textContent = JSON.stringify(data, undefined, 2);
</script>
</body>
</html>
`
const logs = await Log.find({}).
sort('-time').
limit(11)
let middle = JSON.stringify(logs)
res.send(header+middle+footer)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})