-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (37 loc) · 1.39 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import mongoose from "mongoose";
import path from "path";
import { PORT, URI } from "./config/index.js";
import App from "./routes/index.js";
import { fileURLToPath } from 'url';
const server = express();
// Define CORS options
const corsOptions = {
origin: ['http://localhost:3000', 'https://next-form-app-pi.vercel.app'], // List of allowed origins
credentials: true, // Allow credentials
optionsSuccessStatus: 200 // Some legacy browsers (IE11, various SmartTVs) choke on 204
};
server.use(cors(corsOptions)); // Use CORS with specified options
server.disable("x-powered-by");
server.use(cookieParser());
server.use(express.urlencoded({ extended: false }));
server.use(express.static('public'));
server.use(express.json());
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Set up EJS
server.set('view engine', 'ejs');
server.set('views', path.join(__dirname, 'views'));
mongoose.promise = global.Promise;
mongoose.set("strictQuery", false);
mongoose.connect(URI).then(console.log("Connected to MongoDB"))
.catch((err) => {
console.error("Failed to connect to MongoDB", err);
process.exit(1); // Exit process with an error code
});
server.use(App);
server.listen(PORT, () =>
console.log(`Server is running on port ${PORT}`),
);