This repository was archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
152 lines (138 loc) · 4.19 KB
/
server.js
File metadata and controls
152 lines (138 loc) · 4.19 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Required modules
const express = require("express");
const keys=require("./keys");
const mongoose = require("mongoose");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const cookieSession = require("cookie-session");
const passport = require("passport");
const passportSetup = require("./config/passport-setup");
const session = require("express-session");
const routes = require("./routes");
const authRoutes = require("./routes/auth-routes");
const path = require("path");
const db = require("./models");
require("dotenv").config();
// Setup Express app
const PORT = process.env.PORT || 3001;
const app = express();
const server = require('http').createServer(app);
const io = require("socket.io")(server);
// Configure middleware
app.use(express.urlencoded({extended: true}));
app.use(express.json());
// Serve up static assets
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client", "build")));
}
app.use(
session({
secret: 'codemarks',
resave: false,
saveUninitialized: false
})
)
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
// set up cors to allow us to accept requests from our client
app.use(
cors({
origin: ["http://localhost:3000","https://codemarks-app.herokuapp.com/"],// allow to server to accept request from different origin
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true // allow session cookie from browser to pass through
})
);
// Routes
app.use(routes);
app.use("/auth", authRoutes);
// Mongo DB connection
var MONGODB_URI = process.env.CONNECTION_STRING;
mongoose.connect(MONGODB_URI, {
dbName: "codeMarks",
useUnifiedTopology: true,
useNewUrlParser: true
}, (err) => {
if (err) {
console.log('Failed to connect to MongoDB Atlas');
console.log(err);
} else {
console.log('Successfully connected to MongoDB Atlas');
}
mongoose.connection.db.listCollections().toArray(function(err, names) {
if (err) {
console.log(err);
} else {
console.log('The following collections are visible:');
let j = 1;
names.forEach((e, i, a) => {
console.log(j + ') ' + e.name);
j++;
} );
}
})
});
const authCheck = (req, res, next) => {
if (!req.user) {
res.status(401).json({
authenticated: false,
message: "user has not been authenticated"
});
} else {
next();
}
};
// if it's already login, send the profile response,
// otherwise, send a 401 response that the user is not authenticated
// authCheck before navigating to home page
app.get("/", authCheck, (req, res) => {
res.status(200).json({
authenticated: true,
message: "user successfully authenticated",
user: req.user,
cookies: req.cookies
});
});
// Start API server
server.listen(PORT, function() {
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`);
});
io.on('connection', (socket) => {
let postKey = socket.handshake.query.postKey;
let userKey = socket.handshake.query.userKey;
socket.join(postKey);
socket.postKey = postKey;
socket.userKey = userKey;
console.log('socket opened for ' + (socket.userKey ? ('user ' + socket.userKey) : 'guest'));
db.Comment
.find({ post_id: socket.postKey })
.populate('author')
.sort({ date: -1 })
.then(dbModel => {
console.log(dbModel);
socket.emit('existingComments', JSON.stringify(dbModel));
})
.catch(err => console.log(err));
socket.on('newCommentRequest', (commentData) => {
if (!socket.userKey) return;
commentData.post_id = socket.postKey;
commentData.author = socket.userKey;
db.Comment
.create(commentData)
.then(dbModel => {
// save to post's comments array here
db.Comment
.find({ _id: dbModel._id })
.populate('author')
.then(dbModel2 => {
console.log(dbModel2);
io.in(socket.postKey).emit('newComment', JSON.stringify(dbModel2));
})
})
.catch(err => { console.log(err); });
});
socket.on('disconnect', (socket) => {
// TODO: Is there anything we need to do here?
console.log('socket closed for ' + (socket.userKey ? ('user ' + socket.userKey) : 'guest'));
});
});