-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (69 loc) · 3.71 KB
/
server.js
File metadata and controls
83 lines (69 loc) · 3.71 KB
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
const express = require("express"); // Import Express
const mongoose = require("mongoose"); // Import Mongoose library to connect and work with MongoDB
const nano = require("nano")("http://admin:Qd45jKbY89Z@localhost:5984"); // Import Nano library to connect and work with CouchDB
const cors = require("cors"); // Imported to allow cross-origin requests, in case frontend is added
const path = require("path"); // Imported for working with file paths, in case frontend is added
// Import datamodel (MongoDB) and validation (CouchDB)
const Patient = require("./mongodb_model/Patient");
const validatePatientData = require("./couchdb_validation/validatePatientData");
const app = express(); // Create a new Express application
const PORT = 3000; // Define the port the server will run on
// Connection to databases
const mongodb_URL = "mongodb://localhost:27017/Examensarbete_MongoDB_databas"; // MongoDB URL
const couchdb_DB_NAME = "examensarbete_couchdb_databas"; // CouchDB database name
// Global error handling for uncaught exceptions in the rest of the scripts (in case any error handling is missing)
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
});
// Global error handling for failed promises in the rest of the scripts (if any async/await errors are not caught)
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled rejection at:', promise, 'reason:', reason);
});
// Import route files for operations POST/GET/PUT
const mongoPatientRoutes = require("./routes/mongoPatientRoutes");
const couchPatientRoutes = require("./routes/couchPatientRoutes");
// Middleware
app.use(express.json({ limit: '50mb' })); // Middleware function to transform incoming JSON to JavaScript objects (node.js only understand JavaScript objects). Limit: allows 50 megabyte big JSON body (instead of 100 kilobyte request size, that is deafult for Express)
app.use(cors()); // Allows requests from other addresses, e.g if a web page tries to reach the server (allows cross-origin HTTP requests). Not used since I don´t use a frontend, but server is ready
app.use(express.static(path.join(__dirname))); // Server is prepared to serve frontend files (HTML, CSS, JS) if a frontend is added. Not used since I don´t use a frontend, but server is ready
// Simple test route
app.get("/", (request, response) => {
response.send("Server is up and running!");
});
// Use route files
app.use("/mongo/patient", mongoPatientRoutes);
app.use("/couch/patient", couchPatientRoutes);
// Connect to MongoDB
async function connectMongoDB() {
try {
await mongoose.connect(mongodb_URL);// Try connecting
console.log("Connected to MongoDB");
} catch (error) {
console.error("Could not connect to MongoDB:", error);
process.exit(1); // Stops the server if connection fails
}
}
// Connect to CouchDB
async function connectCouchDB() {
try {
const dbList = await nano.db.list(); // Fetches list of databases
if (!dbList.includes(couchdb_DB_NAME)) {
throw new Error(`CouchDB database '${couchdb_DB_NAME}' does not exist.`);
}
console.log("Connected to CouchDB");
} catch (error) {
console.error("Could not connect to CouchDB:", error);
process.exit(1); // Stops the server if connection fails
}
}
// Start the server after both databases are connected
async function startServer() {
await connectMongoDB(); // Connect to MongoDB
await connectCouchDB(); // Connect to CouchDB
//Server starts listening for HTTP-requests on port 3000 and announces that it´s ready in console
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
// Start the process
startServer();