-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
43 lines (36 loc) · 1.2 KB
/
main.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
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import dotenv from "dotenv";
import morgan from "morgan";
import { Server } from "socket.io";
import { createServer } from "http";
import path from "path";
import { fileURLToPath } from "url";
const app = express();
const server = createServer(app);
const io = new Server(server);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config();
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.static(path.join(__dirname, "/public")));
app.use(morgan("common"));
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());
io.on("connection", (socket) => {
socket.on("send-location", (data) => {
io.emit("receive-location", { id: socket.id, ...data });
});
socket.on("disconnect", () => {
io.emit("user-disconnected", socket.id);
});
console.log("connected", socket.id);
});
app.get("/", (req, res) => {
res.render("index");
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server Port: ${PORT}`));