-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpress.js
71 lines (57 loc) · 1.87 KB
/
express.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import dotenv from "dotenv";
import express from "express";
import cors from "cors";
import path from "path";
import connectToDatabase from "./mongoDB.js";
import cookieParser from "cookie-parser";
import { createServer } from "http";
import { Server } from "socket.io";
import morgan from "morgan";
import routerForgetPassword from "./routes/forgetPassword.js";
import routerAuth from "./routes/auth.js";
import routerPostCRUD from "./routes/postCRUD.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 2000;
const __dirname = path.resolve();
app.use(morgan("dev"));
app.use(
cors({
// origin: ["http://localhost:3000"],
origin: "*",
credentials: true,
})
);
app.use(express.json());
app.use(cookieParser());
connectToDatabase();
app.use("/", express.static(path.join(__dirname, "/web/build")));
// app.get("/", (req, res, next) => {
// res.sendFile(path.join(__dirname, "./web/build/index.html"));
// });
app.use("/api/v1/auth", routerAuth);
app.use("/api/v1/new", routerForgetPassword);
const server = createServer(app);
const io = new Server(server, { cors: { origin: "*", methods: "*" } });
export const socketInstance = io;
app.use("/api/v1/post", routerPostCRUD);
app.use("/**", (req, res) => {
// res.redirect("/")
res.sendFile(path.join(__dirname, "/web/build/index.html"));
});
// app.listen(PORT, () =>
// console.log(`Example app listening on http://localhost:${PORT}`)
// );
io.on("connection", (socket) => {
console.log("New client connected with id: ", socket.id);
// to emit data to a certain client
// socket.emit("topic 1", "some data");
// collecting connected users in a array
// connectedUsers.push(socket)
socket.on("disconnect", (message) => {
console.log("Client disconnected with id: ", message);
});
});
server.listen(PORT, function () {
console.log(`server is running on http://localhost:${PORT}`);
});