-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.mjs
34 lines (28 loc) · 951 Bytes
/
server.mjs
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
import express from "express";
import cors from "cors";
import { config } from "dotenv";
import { PORT } from "./config/index.mjs";
import connect from "./db/index.mjs";
import apiAuth from "./routes/auth.mjs";
import apiRecipes from "./routes/recipe.mjs";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
const __fileName = fileURLToPath(import.meta.url);
const __dirname = dirname(__fileName);
config();
const app = express();
app.use(cors("*"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api/auth", apiAuth);
app.use("/api/recipe", apiRecipes);
if (process.env.NODE_ENV === "production") {
app.use(express.static("build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "build", "index.html"));
});
} // Serve the build files if the app is in production mode
app.listen(PORT, () => {
connect();
console.log(`Server started at port ${PORT}`);
});