forked from misa-j/social-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (122 loc) · 3.99 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const createError = require("http-errors");
const express = require("express");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const socket_io = require("socket.io");
const jwt = require("jsonwebtoken");
const path = require("path");
const logger = require("morgan");
const mongoose = require("mongoose");
const fs = require("fs");
require("dotenv").config({ path: "variables.env" });
// connect to DB
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on("error", (err) => {
console.error(err.message);
});
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
mongoose.set("autoIndex", false);
require("./models/Post");
require("./models/User");
require("./models/Comment");
require("./models/CommentReply");
require("./models/CommentReplyLike");
require("./models/CommentLike");
require("./models/PostLike");
require("./models/Following");
require("./models/Followers");
require("./models/Notification");
require("./models/ChatRoom");
require("./models/Message");
const app = express();
const io = socket_io();
const userController = require("./controllers/userController");
app.io = io;
app.set("socketio", io);
io.use((socket, next) => {
if (socket.handshake.query && socket.handshake.query.token) {
const token = socket.handshake.query.token.split(" ")[1];
jwt.verify(token, process.env.JWT_KEY, (err, decoded) => {
if (err) return next(new Error("Authentication error"));
socket.userData = decoded;
next();
});
} else {
next(new Error("Authentication error"));
}
}).on("connection", (socket) => {
// Connection now authenticated to receive further events
socket.join(socket.userData.userId);
io.in(socket.userData.userId).clients((err, clients) => {
userController.changeStatus(socket.userData.userId, clients, io);
//console.log(clients);
});
socket.on("typing", (data) => {
socket.to(data.userId).emit("typing", { roomId: data.roomId });
});
socket.on("stoppedTyping", (data) => {
socket.to(data.userId).emit("stoppedTyping", { roomId: data.roomId });
});
socket.on("disconnect", () => {
socket.leave(socket.userData.userId);
io.in(socket.userData.userId).clients((err, clients) => {
userController.changeStatus(socket.userData.userId, clients, io);
//console.log(clients);
});
});
});
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 200, // limit each IP to 200 requests per windowMs
});
const postsRouter = require("./routes/post");
const usersRouter = require("./routes/user");
const commentsRouter = require("./routes/comment");
const notificationRouter = require("./routes/notification");
const chatRouter = require("./routes/chat");
app.use(helmet());
if (process.env.NODE_ENV === "production") {
app.use(limiter);
app.use(
logger("common", {
stream: fs.createWriteStream("./access.log", { flags: "a" }),
})
);
} else {
app.use(logger("dev"));
}
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use("/api/post/", postsRouter);
app.use("/api/user/", usersRouter);
app.use("/api/comment/", commentsRouter);
app.use("/api/notification/", notificationRouter);
app.use("/api/chat/", chatRouter);
app.get("/auth/reset/password/:jwt", function (req, res) {
return res.status(404).json({ message: "go to port 3000" });
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
// res.locals.message = err.message;
// res.locals.error = process.env.NODE_ENV === "development" ? err : {};
console.log(err);
// render the error page
res.status(err.status || 500);
res.json({
error: {
message: err.message,
},
});
});
module.exports = app;