-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (55 loc) · 1.49 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
"use strict";
const Hapi = require("@hapi/hapi");
const inert = require("@hapi/inert");
const path = require("path");
const dotenv = require("dotenv");
const colors = require("colors");
const laabr = require("laabr");
const connectDB = require("./config/databaseConnection");
const userRoutes = require("./routes/userRoute");
const testRoutes = require("./routes/testRoute");
dotenv.config({ path: "./config/config.env" });
const PORT = process.env.PORT || 5001;
const server = new Hapi.server({
port: PORT,
host: "localhost",
routes: {
cors: {
origin: ["*"],
headers: ["Accept", "Content-Type"],
additionalHeaders: ["X-Requested-With"],
},
},
});
server.route([...userRoutes, ...testRoutes]);
(async () => {
try {
// laabr
if (process.env.NODE_ENV === "development") {
await server.register({
plugin: laabr,
options: {
formats: { onPostStart: ":time :start :level :message" },
tokens: { start: () => "[start]" },
indent: 0,
},
});
}
// inert provides new handler methods for serving static files and directories
await server.register(inert);
// Database connection
await connectDB();
// Start server
await server.start();
console.log(
`Server is running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow
.bold
);
} catch (err) {
console.error(err);
}
})();
process.on("unhandledRejection", (err) => {
console.log(err);
process.exit(1);
});