58 lines
1.2 KiB
JavaScript
58 lines
1.2 KiB
JavaScript
|
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());
|
||
|
|
||
|
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`);
|
||
|
});
|
||
|
|