-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (47 loc) · 1.27 KB
/
server.js
File metadata and controls
58 lines (47 loc) · 1.27 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
const express = require("express");
const dotenv = require("dotenv");
const connectDB = require("./config/db");
const admin = require("firebase-admin");
const path = require("path");
const cors = require("cors");
//env vars
dotenv.config({ path: "config/config.env" });
//Connect to database
connectDB();
// Init Firebase Admin SDK
if (!admin.apps.length) {
const serviceAccountPath = path.join(
__dirname,
"config/firebaseServiceAccountKey.json"
);
admin.initializeApp({
credential: admin.credential.cert(require(serviceAccountPath)),
});
}
//Route files
const campgrounds = require("./routes/campgrounds");
const auth = require("./routes/auth");
const bookings = require("./routes/bookings");
const app = express();
app.use(cors());
//Body parser
app.use(express.json());
app.use("/api/v1/campgrounds", campgrounds);
app.use("/api/v1/auth", auth);
app.use("/api/v1/bookings", bookings);
const PORT = process.env.PORT || 5000;
const server = app.listen(
PORT,
console.log(
"Server running in ",
process.env.NODE_ENV,
" mode on port ",
PORT
)
);
//Handle unhandled promise rejections
process.on("unhandledRejection", (err, promise) => {
console.log(`Error: ${err.message}`);
//Close server & exit process
server.close(() => process.exit(1));
});