-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
64 lines (55 loc) · 1.68 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
import express from "express";
import cors from "cors";
import orderRouter from "./routes/orderRouter.js";
import menuRouter from "./routes/menuRouter.js";
import inventoryRouter from "./routes/inventoryRouter.js";
import restaurantRouter from "./routes/restaurantRouter.js";
import authRouter from "./routes/authRouter.js";
import staffRouter from "./routes/staffRouter.js";
import postgres from "pg";
import dotenv from "dotenv";
import Data from "./api/Data.js";
import corsOptions from "./config/origins.js";
import helmet from "helmet";
const Database = new Data();
dotenv.config();
const { Pool } = postgres;
const app = express();
app.use(helmet());
// using cors here
app.use(cors(corsOptions));
// Error handling middleware
app.use((err, req, res, next) => {
if (err.message === "Not allowed by CORS") {
res.status(403).send({ error: "Not allowed by CORS" });
} else {
next(err);
}
});
const pool = new Pool({
host: process.env.PSQL_HOST,
port: process.env.PSQL_PORT,
database: process.env.PSQL_DATABASE,
user: process.env.PSQL_USER,
password: process.env.PSQL_PASSWORD,
ssl: { rejectUnauthorized: false },
});
process.on("SIGINT", function () {
pool.end();
console.log("Application successfully shutdown");
process.exit(0);
});
app.set("view engine", "ejs");
const PORT = process.env.PORT || 8080;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/orders", orderRouter);
app.use("/menu", menuRouter);
app.use("/inventory", inventoryRouter);
app.use("/restaurant", restaurantRouter);
app.use("/auth", authRouter);
app.use("/staff", staffRouter);
app.listen(PORT, () => {
console.log(`Server is started on port ${PORT}`);
});
export { pool, Database };