server.js
1.01 KB
const path = require("path");
const express = require("express");
const dotenv = require("dotenv").config();
const cors = require("cors");
const { errorHandler } = require("./middleware/errorMiddleware");
const connectDB = require("./config/db");
const port = process.env.PORT || 8080;
connectDB();
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api/users", require("./routes/userRoutes"));
app.use("/api/weather", require("./routes/weatherRoutes"));
app.use("/api/posts", require("./routes/postRoutes"));
// Connect client(frontend)
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "../client/build")));
app.get("*", (req, res) =>
res.sendFile(
path.resolve(__dirname, "../", "client", "build", "index.html")
)
);
} else {
app.get("/", (req, res) => res.send("Set env to production"));
}
app.use(errorHandler);
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});