83 lines
2.1 KiB
JavaScript
83 lines
2.1 KiB
JavaScript
import express from "express";
|
|
import mongoose from "mongoose";
|
|
import mysql from "mysql2";
|
|
import cors from "cors";
|
|
import morgan from "morgan";
|
|
import session from "express-session";
|
|
import userRouter from "./routes/user.js";
|
|
import propertyRouter from "./routes/property.js";
|
|
import mysqlRouter from "./routes/mysqlproperty.js";
|
|
import adminRouter from "./routes/admin.js"
|
|
import dotenv from "dotenv";
|
|
// import bcrypt from "bcryptjs";
|
|
// import adminModal from "./models/admin.js";
|
|
|
|
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());
|
|
|
|
// app.use(cors({
|
|
// origin: 'http://67.225.129.127:81', // Allow requests from your frontend
|
|
// methods: 'GET,POST', // Specify allowed methods
|
|
// }));
|
|
app.use(express.static('dist'));
|
|
|
|
app.use(
|
|
session({
|
|
secret: process.env.SECRET_KEY,
|
|
resave: false,
|
|
saveUninitialized: true,
|
|
})
|
|
);
|
|
|
|
app.use("/users", userRouter);
|
|
app.use("/properties", propertyRouter);
|
|
app.use("/mysql", mysqlRouter); // Use MySQL routes
|
|
app.use("/admin", adminRouter);
|
|
|
|
|
|
|
|
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 MONGO database");
|
|
})
|
|
.catch((error) => {
|
|
console.log(`${error} did not connect`);
|
|
});
|
|
|
|
|
|
// const createAdminUser = async () => {
|
|
// const hashedPassword = await bcrypt.hash("Uer$99#KKma-hi19", 10);
|
|
// const admin = new adminModal({ username: "admin", password: hashedPassword });
|
|
// await admin.save();
|
|
// console.log("Admin user created.", admin);
|
|
// mongoose.disconnect();
|
|
// };
|
|
|
|
// createAdminUser();
|
|
|