-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
49 lines (40 loc) · 1.16 KB
/
index.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
46
47
48
49
import http from "http";
import express from "express";
import dotenv from "dotenv";
import morgan from "morgan";
import path from "path";
import cors from "cors";
import { Server } from "socket.io";
import db from "./backend/src/mongo.js";
import apiRouter from "./backend/src/api.js";
import socket from "./backend/src/socket.js";
dotenv.config();
const { PORT, MONGO_URL } = process.env;
const port = PORT || 2022;
db.once("open", () => {
console.log(`MongoDB connected at ${MONGO_URL}`);
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: `http://localhost:${port}`,
methods: ["GET", "POST"],
},
});
app.use(express.json());
app.use(cors());
app.use(morgan("dev"));
app.use(express.static(path.join(process.cwd(), "frontend/build")));
app.use(function (request, response, next) {
request.io = io;
next();
});
app.use("/api", apiRouter);
app.get("/*", (req, res) => {
res.sendFile(path.join(process.cwd(), "frontend/build", "index.html"));
});
socket(io);
server.listen(port, () =>
console.log(`App listening at http://localhost:${port}`)
);
});