-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (52 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Importing Express Router
const express = require("express");
const app = express();
// Importing cors
const cors = require("cors");
app.use(cors());
// socket.io setup
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
transports: ["polling"],
cors: {
origin: "*",
},
});
// socket connection
io.on("connection", (client) => {
// console.log("socket ID:" + client.id);
client.on("join", (data) => {
console.log(data);
});
client.on("disconnect", () => {
client.emit("message", "Bye from server");
});
});
module.exports = { io };
// use cors to allow cross origin resource sharing
app.use(function (req, res, next) {
req.io = io;
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
// Routes for the API
const Terminal = require("./Terminal");
// use Terminal.js to handle the terminal requests
app.use("/terminal", Terminal);
app.get("/", (req, res) => {
res.send("Just a Server Route");
});
// using PORT from env file or 5000 for local development
const PORT = process.env.PORT || 5000;
// System Information
const os = require("os");
const dotEnv = require("dotenv");
dotEnv.config();
// ----- connect to server -----
server.listen(PORT, () => {
console.log(`🚀 http://${os.hostname()}:${PORT}`);
});