import express from "express";
import cors from "cors";
import apiRouter from "./routes/api.js";

const app = express();
const port = 7001;

app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.json());

app.use("/api", apiRouter);

app.get("/", (req, res) => {
  res.render("index.ejs", { title: port });
});

app.listen(port, () => {
  console.log(`Server is running on port: ${port}`);
});

// error handling code
app.use((err, req, res, next) => {
  // Log the error stack and send a generic error response
  console.error(err.stack);
  res.status(500).send("Something broke!");
});
