-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (49 loc) · 1.55 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
const express = require("express");
const path = require("path");
const logger = require("morgan");
require("dotenv").config();
const database = require("./config/database");
// database.connect();
const userRouter = require("./routes/usersRouter");
const petRouter = require("./routes/petsRouter");
const roomRouter = require("./routes/roomsRouter");
const bookingRouter = require("./routes/bookingsRouter");
//! add other routers here
const jwt = require("jsonwebtoken");
const app = express();
const port = process.env.PORT || 3000;
app.use(logger("dev"));
app.use(express.json());
app.use(express.static(path.join(__dirname, "dist")));
// app.get("/api", (req, res) => {
// res.json({ msg: "Hi" });
// });
const isLoggedIn = (req, res, next) => {
try {
const { authorization } = req.headers;
const token = authorization.split(" ")[1];
const decode = jwt.verify(token, process.env.JWT_SECRET);
if (decode) {
res.locals.user = decode.user;
next();
} else {
res.status(403).json({ message: "Sorry" });
}
} catch (error) {
res.status(403).json({ error });
}
};
app.use("/api/users", userRouter);
app.use("/api/pets", petRouter);
app.use("/api/rooms", roomRouter);
app.use("/api/bookings", bookingRouter);
//! add other routes here
app.get("/*", function (req, res) {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
setTimeout(() => {
app.listen(port, () => {
console.log(`Express listening on port ${port}`);
});
}, 1000); // add a delay of 3 seconds before starting the server
module.exports = app;