-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (68 loc) · 2.09 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
const express = require("express");
const cors = require("cors");
const path = require("path");
const fs = require("fs");
const http = require("http");
const socketIO = require("socket.io");
const { addUser, removeUser } = require("./ws/index");
require("dotenv").config();
const morgan = require("morgan");
const notificationsRouter = require("./routes/notifications");
const notFound = require("./middlewares/notFound");
require("./db");
const app = express();
const Server = http.createServer(app);
const io = socketIO(Server, {
cors: "*",
});
io.on("connection", (socket) => {
socket.on("joined", (data) => {
addUser(data, socket.id);
});
socket.on('disconnect', () => {
console.log('🔥: A user disconnected');
removeUser(socket.id)
});
});
const authRouter = require("./routes/auth");
const postRouter = require("./routes/posts");
const userRouter = require("./routes/users");
app.use(cors());
app.use(express.json());
app.use(morgan("dev"));
app.use((req, res, next) => {
req.io = io;
next();
});
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client", "dist")));
}
app.use("/images", express.static(path.join(__dirname, "uploads")));
if (!fs.existsSync(path.join(__dirname, "uploads"))) {
console.log("created uploads folder");
fs.mkdirSync(path.join(__dirname, "uploads"));
}
app.use("/api/auth", authRouter);
app.use("/api/posts", postRouter);
app.use("/api/users", userRouter);
app.use("/api/notifications", notificationsRouter);
app.use((err, _req, res, _next) => {
const status = err.statusCode || 500;
const message = err.message || "Internal Server Error";
const obj = {
success: false,
status,
message,
};
if(process.env.NODE_ENV !== 'production') obj['stack'] = err.stack
res.status(status).send(obj);
});
app.use('*', (req, res, next) => {
res.sendFile(path.join(__dirname, 'client', 'dist', 'index.html'))
})
app.use(notFound);
// module.exports = app
const PORT = process.env.PORT || 5000;
Server.listen(PORT, () => {
console.log(`Server started in ${process.env.NODE_ENV} `, PORT);
});