-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
137 lines (118 loc) · 3.74 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
129
130
131
132
133
134
135
136
137
const express = require("express");
const app = express();
var http = require("http").Server(app);
module.exports = {
http,
};
const morgan = require("morgan");
var bodyParser = require("body-parser");
const dotenv = require("dotenv");
const appError = require("./utils/appError");
const globalErrorHandler = require("./controllers/errorController");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
const hpp = require("hpp");
var cors = require("cors");
const limiter = rateLimit({
// pour maximiser 100 requetes / heure pour le meme @IP
max: 100,
windowMs: 60 * 60 * 1000,
message:
"Trop de requetes de cette adresse IP, veuillez réessayer dans une heure!",
});
var http = require("http").Server(app);
module.exports = {
http,
};
// app.use("/API", limiter);
// set security HTTP headers
app.use(cors());
app.use(helmet());
process.on("uncaughtException", (err) => {
console.log(err.name, err.message);
console.log("UNHADLED REJECTION! Shuting down ...");
process.exit(0);
});
dotenv.config({
path: "./config.env",
});
const mongoose = require("mongoose");
const DB = process.env.DATABASE.replace(
"<password>",
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => {
console.log("db connecction successful !");
});
if ((process.env.NODE_ENV = "developement")) {
app.use(morgan("dev"));
}
// Serving static file from public
app.use(express.static("public"));
//partie bodyParser
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(bodyParser.json());
//fin partie bodyParser
// data sanitization agaist NoSQL query injection
app.use(mongoSanitize());
// data sanitization agaist XSS
app.use(xss());
// prevent paremeter poulution
app.use(hpp());
// ###################### Routes ######################
const authRoutes = require("./routes/authRoutes");
const agentRoutes = require("./routes/agentRoutes");
const interventionRoutes = require("./routes/interventionRoutes");
const statistiquesRoutes = require("./routes/statistiquesRoutes");
const appelRoutes = require("./routes/appelRoutes");
const treeRoutes = require("./routes/treeRoutes");
const planningRoutes = require("./routes/planningRoutes");
const teamRoutes = require("./routes/teamRoutes");
const uniteRoutes = require("./routes/uniteRoutes");
const enginRoutes = require("./routes/enginRoutes");
const hospitalRoutes = require("./routes/hospitalRoutes");
app.use("/API/", authRoutes);
app.use("/API/", agentRoutes);
app.use("/API/", interventionRoutes);
app.use("/API/", statistiquesRoutes);
app.use("/API/", appelRoutes);
app.use("/API/", treeRoutes);
app.use("/API/", planningRoutes);
app.use("/API/", teamRoutes);
app.use("/API/", uniteRoutes);
app.use("/API/", enginRoutes);
app.use("/API/", hospitalRoutes);
app.use(require("./socket.js").router);
const io = require("./socket.js").io;
app.all("*", (req, res, next) => {
next(new appError(`Can't find ${req.originalUrl} on this server`, 404));
});
app.use(globalErrorHandler);
// ###################### FIN Routes ######################
const PORT = process.env.PORT || 30001;
const server = http.listen(PORT, "0.0.0.0", function () {
console.log("Server is running on : " + process.env.LOCALHOST + ":" + PORT);
});
// const server = http.listen(PORT, process.env.LOCALHOST, function () {
// console.log("Server is running on : " + process.env.LOCALHOST + ":" + PORT);
// });
process.on("unhandledRejection", (err) => {
console.log(err.name, err.message);
console.log("UNHADLED REJECTION! Shuting down ...");
server.close(() => {
process.exit(0);
});
});