-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
105 lines (86 loc) · 3.38 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
const express = require("express");
require('dotenv').config
const mongoose = require("mongoose")
const userRoutes = require('./routes/userRoutes')
const cors = require('cors')
const cookieParser = require('cookie-parser')
const sessions = require('express-session')
const auth = require("./auth");
const http = require('http')
const { Server } = require('socket.io')
const app = express();
require("dotenv").config();
const port = process.env.PORT || 3001;
// store session on server similar to cookies: https://www.section.io/engineering-education/session-management-in-nodejs-using-expressjs-and-express-session/
const oneDay = 1000 * 60 * 60 * 24;
app.use(sessions({
secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
saveUninitialized:true,
cookie: { maxAge: oneDay },
resave: false
}));
//https://expressjs.com/en/resources/middleware/cors.html
//https://www.npmjs.com/package/cors
app.options('*', cors())
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // https://www.section.io/engineering-education/session-management-in-nodejs-using-expressjs-and-express-session/
app.use(express.static('./client/build'))
// cookie parser middleware
app.use(cookieParser());
// Keep routes neat without repeat code: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes
app.use('/api/auth',userRoutes)
app.get("api/test", (req,res) => {
res.json({ result: "success" })
})
// Connect to mongodb (https://www.bezkoder.com/react-node-express-mongodb-mern-stack/)
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log("DB Connection Successful")
console.log(process.env.MONGODB_URI)
}).catch((err) => {
console.log(err.message)
})
// Socket.io
const server = http.createServer(app)
// Connect socket.io server to express server
const io = new Server(server, {
cors: {
origin: "http://localhost:3000", // Tells server which url is making calls to socket.io server (React App location)
methods: ["GET", "POST"] // what requests are allowed
}
})
// check for user connecting to socket server. on("<event name>") listens for event with event name
io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`); // log the connected user's id
// join room functionality
socket.on("join_room", (data) => { // where data is the room id
socket.join(data)
console.log(`User with ID: ${socket.id} joined room: ${data}`)
})
socket.on("send_message", (data) => {
// use to(data.room) to tell which room to send to
socket.to(data.room).emit("receive_message", data)
})
// disconnect functionality
socket.on("disconnect", () => {
console.log("User disconnected", socket.id)
})
})
// free endpoint
app.get("/free-endpoint", (request, response) => {
response.json({ message: "You are free to access me anytime" });
});
// authentication endpoint
app.get("/auth-endpoint", auth, (request, response) => {
response.json({ message: "You are authorized to access me" });
});
// https://stackoverflow.com/questions/41534370/socket-io-wont-work-connect-in-reactjs-app
server.listen(port, () => {
console.log(`Server is running on port: ${port}`)
})