forked from 1s-0s/Gitto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
119 lines (101 loc) · 2.94 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const express = require("express");
const passport = require("passport");
const chalk = require("chalk");
const bodyParser = require("body-parser");
const methodOverride = require("method-override");
const auth = require("./routes/auth");
const userinfo = require("./routes/userinfo");
const mongoose = require("mongoose");
const morgan = require("morgan"); //HTTP logger
const cookieSession = require("cookie-session");
//!------socket.io------
const cors = require("cors");
const http = require("http");
const socketio = require("socket.io")(3001, {
cors: {
origin: "*",
},
});
//!---------------------
//dotenv configuration
require("dotenv").config();
//database
require("./models/User");
//github authentication
require("./passport/githubConfig");
const PORT = process.env.PORT || 5000;
//MONGOOSE CONNECTION
mongoose.connect(
process.env.MONGODB_URI,
{ useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false },
(err) => {
if (!err) console.log(chalk.hex("#BF68F6").bold("database is connected"));
else console.log(chalk.red("Error:", err));
}
);
const app = express();
// HTTP request logger
app.use(morgan("tiny"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(express.static("public"));
//?cookie session
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [process.env.COOKIE_SESSION_KEY],
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use("/auth", auth);
app.use("/userinfo", userinfo);
//! Heroku Deloyment
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (request, response) => {
response.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
app.listen(PORT, () => {
console.log(chalk.hex("#448EF6").bold("Server is running boiss"));
});
//!---socket IO----
app.use(cors());
socketio.on("connection", (socket) => {
// either with send()
socket.send("Connecting established from server");
// or with emit() and custom event names
socket.emit("greetings! welcome to the room", Buffer.from([4, 3, 3, 1]));
// handle the event sent with socket.send()
socket.on("message", (data) => {
console.log(data);
});
// handle the event sent with socket.emit()
socket.on("salutations", (elem1, elem2, elem3) => {
console.log(elem1, elem2, elem3);
});
socket.on("join", ({ name, room }) => {
console.log("name: ", name, " room: ", room);
})
});
// const server = http.createServer(app);
// const io = socketio(server, {
// cors: {
// origin: "*",
// methods: ["GET", "POST"],
// credentials: true,
// },
// });
// io.on("connection", (socket) => {
// console.log("connection established successfully");
// socket.on("join",()=>{
// console.log("joined");
// })
// socket.on("disconnect", () => {
// console.log("user has left");
// socket.close();
// });
// });
//!----------------