-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (57 loc) · 1.93 KB
/
index.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
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv").config();
const connectToDB = require("./config/database");
const cors = require("cors");
const session = require("express-session");
const passport = require("passport");
const path = require("path");
const authRoutes = require("./routes/auth");
const adminRoutes = require("./routes/admin");
const companyRoutes = require("./routes/company");
const customerRoutes = require("./routes/customer");
const staffRoutes = require("./routes/staff");
const storeroutes = require("./routes/store");
const PORT = process.env.PORT || 8200;
const app = express();
connectToDB();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "build")));
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
/* app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
next();
}); */
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use("/api/auth", authRoutes);
app.use("/api/admin", adminRoutes);
app.use("/api/company", companyRoutes);
app.use("/api/customer", customerRoutes);
app.use("/api/staff", staffRoutes);
app.use("/store", storeroutes);
app.use((error, req, res, next) => {
console.log(error);
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(status).json({ message: message, data: data });
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.listen(PORT, () => console.log("Server connected on PORT:", PORT));