-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
131 lines (107 loc) · 3.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Importing all required modules
import express from "express";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import Connection from "./database/db.js";
import rateLimit from "express-rate-limit";
import hsts from "hsts";
import { router } from "./routes.js";
import saveAnimalLiveLocation from "./routeFunctions/saveAnimalLiveLocation.js"
import { Server } from "socket.io";
import https from "https";
import path from "path";
import fs from "fs";
import { fileURLToPath } from 'url';
import helmet from "helmet";
import cluster from "cluster";
import os from "os";
dotenv.config();
// file directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const numCpu = os.cpus().length;
const app = express();
// Server
// const server = https.createServer({
// key: fs.readFileSync(path.join(__dirname, 'sslCertificate', 'key.pem')),
// cert: fs.readFileSync(path.join(__dirname, 'sslCertificate', 'cert.pem'))
// }, app);
// let io = new Server(server);
const limiter = rateLimit({
windowMs: 5 * 1000,
max: 25,
message: {
code: 429,
message: 'too many requests'
},
standardHeaders: true,
legacyHeaders: false,
})
//declaring port and database url
const port = process.env.PORT || 3000;
const username = process.env.DB_USERNAME;
const password = process.env.DB_PASSWORD;
const url = `mongodb+srv://${username}:${password}@mongodatabase.slom4qc.mongodb.net/forestApp?retryWrites=true&w=majority`;
Connection(process.env.MONGODB_URI || url);
// Generating frequently changing locations
const generateNextLocation = (obj) => {
return {
Longitude: obj.Longitude + 0.000001,
Latitude: obj.Latitude + 0.000001
}
}
// declaring middleware functions
app.use(bodyParser.json({ limit: '1mb' }));
app.set("view engine", "ejs"); //ejs as templating engine
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public")); //static files in public directory
app.use(limiter);
app.use(helmet());
app.use(hsts({
// Limiting payload size
maxAge: 31536000,
includeSubDomains: true,
preload: true
}));
app.use('/', router);
app.get("/",(req,res)=>{
console.log(`ok... ${process.pid}`);
res.status(200).send("<h1>SMART Forest App Backend</h1>")
})
// cluster.worker.kill();
// res.sendFile(path.join(__dirname, 'home.html'));
// socket connection for live data streaming
app.on("connection", (socket) => {
var locationArray = [];
socket.on("sendLiveLocation", (startData) => {
// console.log(startData);
const location = generateNextLocation({ longitude: 25.4, Latitude: 78.22 })
let interval = setInterval(() => {
location = generateNextLocation(location)
locationArray.push(location);
socket.emit("location", location)
}, 1000);
socket.on("stopLiveLocation", () => {
clearInterval(interval);
saveAnimalLiveLocation(startData, locationArray);
})
});
});
// // using multiple CPU cores to boost speed
// if (cluster.isMaster) {
// for (let i = 0; i < numCpu; i++) {
// cluster.fork();
// }
// cluster.on('exit', (worker, code, signal) => {
// console.log(`worker ${worker.process.pid} died`);
// cluster.fork();
// })
// }
// else {
// server.listen(port, () => {
// console.log(`>> Server ${process.pid} started successfully at port ${port}`);
// });
// }
app.listen(port, () => {
console.log(`>> Server started successfully at port ${port}`);
});