2024-09-04 00:52:26 +00:00
|
|
|
import express from "express";
|
|
|
|
import mongoose from "mongoose";
|
|
|
|
import cors from "cors";
|
|
|
|
import morgan from "morgan";
|
|
|
|
import session from "express-session";
|
|
|
|
import userRouter from "./routes/user.js";
|
|
|
|
import dotenv from "dotenv";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
app.use(morgan("dev"));
|
|
|
|
app.use(express.json({ limit: "30mb", extended: true }));
|
|
|
|
app.use(express.urlencoded({ limit: "30mb", extended: true }));
|
|
|
|
app.use(cors());
|
|
|
|
|
2024-09-04 06:49:59 +00:00
|
|
|
// app.use(cors({
|
|
|
|
// origin: 'http://67.225.129.127:81', // Allow requests from your frontend
|
|
|
|
// methods: 'GET,POST', // Specify allowed methods
|
|
|
|
// }));
|
|
|
|
|
2024-09-04 00:52:26 +00:00
|
|
|
app.use(
|
|
|
|
session({
|
|
|
|
secret: process.env.SECRET_KEY,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
app.use("/users", userRouter);
|
|
|
|
|
|
|
|
app.get("/", (req, res) => {
|
|
|
|
res.send("Welcome to EF-API");
|
|
|
|
});
|
|
|
|
|
|
|
|
const port = process.env.PORT || 5000;
|
|
|
|
|
|
|
|
|
|
|
|
// Connect to the database
|
|
|
|
const dbConnectionPromise = mongoose.connect(process.env.DB_ACCESS, {
|
|
|
|
// useNewUrlParser: true,
|
|
|
|
// useUnifiedTopology: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start HTTP server
|
|
|
|
const startHttpServer = () => {
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Server is running on port ${port} (HTTP)`);
|
|
|
|
});
|
|
|
|
}; startHttpServer();
|
|
|
|
|
|
|
|
// Handle database connection success/failure
|
|
|
|
dbConnectionPromise
|
|
|
|
.then(() => {
|
|
|
|
console.log("Connected to the database");
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(`${error} did not connect`);
|
|
|
|
});
|
|
|
|
|