-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
47 lines (40 loc) · 1.2 KB
/
app.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
import express from "express";
import cors from "cors";
import db from "./database/db.js";
import apiRouter from "./routes/api.route.js";
import alertRouter from "./routes/alert.route.js";
import { notFound, errorHandler } from "./middlewares/error.middleware.js";
import path from "path";
import cookieParser from "cookie-parser";
const app = express();
const PORT = process.env.PORT || 5000;
db.connect()
.then(() => {
console.log("Database is connected");
})
.catch((err) => {
if (err) return console.error(err);
});
app.use(cors());
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/api", apiRouter);
app.use("/alert", alertRouter);
if (process.env.NODE_ENV === "production") {
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, "/client/dist")));
app.get("*", (req, res) =>
res.sendFile(path.resolve(__dirname, "client", "dist", "index.html"))
);
} else {
app.get("/", (req, res) => {
res.send("API is running");
});
}
app.use(notFound);
app.use(errorHandler);
app.listen(PORT, (err) => {
if (err) return console.error(err);
console.log(`Server started listening at port ${PORT}`);
});