-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
56 lines (46 loc) · 1.63 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
// Import necessary libraries and modules
import bodyParser from "body-parser";
import cors from "cors";
import * as dotenv from "dotenv";
import express from "express";
import passport from "passport";
import passportConfig from "./config/passport.js";
import connect from "./database/mongdb.js";
import routes from "./routes/index.js";
import path from "path";
// Load environment variables from a .env file
dotenv.config();
// Set the server's port, defaulting to 4000 if not specified in the environment
const PORT = process.env.PORT || 4000;
// Create an Express application
const app = express();
// Middleware: Enable CORS for cross-origin requests
app.use(cors());
// Middleware: Parse incoming JSON requests
app.use(bodyParser.json());
// Middleware: Initialize Passport for authentication
app.use(passport.initialize());
passportConfig(passport);
// Use the defined routes for routing
app.use("/", routes);
// Connect to the MongoDB database
await connect();
// Deployment configuration
const __dirname1 = path.resolve();
if (process.env.NODE_ENV === "production") {
// Serve static files from the 'client/build' directory
app.use(express.static(path.join(__dirname1, "./client/build")));
// Handle all other routes by serving the 'index.html' file
app.get("*", (req, res) =>
res.sendFile(path.resolve(__dirname1, "./client", "build", "index.html"))
);
} else {
// In development mode, respond with a simple message for the root route
app.get("/", (req, res) => {
res.send("API is running..");
});
}
// Start the server and listen on the specified ports
app.listen(PORT, () => {
console.log(`Server is running at ${PORT}`);
});