-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (137 loc) · 4.42 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const express = require("express");
const app = express();
// cors to resolve error
const cors = require("cors");
const PORT = process.env.PORT || 8800;
app.use(cors());
// // socket
// const socketIO = require("socket.io");
// const server = express()
// .use(app)
// .listen(8900, () => console.log("Listening Socket on 8900"));
// const io = socketIO(server);
// import libraries
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");
// multer for uploading images
const multer = require("multer");
const router = express.Router();
const path = require("path");
// Route Configs
const userRoute = require("./routes/users");
const authRoute = require("./routes/auth");
const postRoute = require("./routes/posts");
const conversationRoute = require("./routes/conversations");
const messageRoute = require("./routes/messages");
// setup dotenv
dotenv.config();
// mongoose connection
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true }, () => {
console.log("connected to MongoDB Cloud!");
});
// set REST API path to assets for access from front-end client
// so if user goes to localhost:8800/images/ad.png -> direct user to localhost:8800/public/images/ad.png
app.use("/images", express.static(path.join(__dirname, "public/images")));
// Middleware
app.use(express.json());
app.use(helmet());
app.use(morgan("common"));
// Base examples - not used since we are making Rest API
// app.get("/", (req, res) => {
// res.send("Welcome to Home Page");
// });
// app.get("/users", (req, res) => {
// res.send("Welcome to Users Page");
// });
// MULTER - storage and file upload
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "public/images");
},
filename: (req, file, cb) => {
// req.body.name is coming from clientside user post
cb(null, req.body.name);
},
});
const upload = multer({ storage: storage });
app.post("/upload", upload.single("file"), (req, res) => {
try {
return res.status(200).json("File uploaded successfully");
} catch (error) {
console.error(error);
}
});
// Set routes
app.use("/auth", authRoute);
app.use("/users", userRoute);
app.use("/posts", postRoute);
app.use("/conversations", conversationRoute);
app.use("/messages", messageRoute);
//////////////////////////////////////////
// SOCKET IO
//////////////////////////////////////////
// const io = require("socket.io")(8900, {
// cors: {
// origin: "http://localhost:3000",
// },
// });
// let users = [];
// const addUser = (userId, socketId) => {
// !users.some((user) => user.userId === userId) &&
// users.push({ userId, socketId });
// };
// const removeUser = (socketId) => {
// users = users.filter((user) => user.socketId !== socketId);
// };
// const getUser = (userId) => {
// return users.find((user) => user.userId === userId);
// };
// io.on("connection", (socket) => {
// //when connect
// console.log("a user connected.");
// //take userId and socketId from user
// socket.on("addUser", (userId) => {
// addUser(userId, socket.id);
// io.emit("getUsers", users);
// });
// //send and get message
// socket.on("sendMessage", ({ senderId, receiverId, text }) => {
// const user = getUser(receiverId);
// io.to(user.socketId).emit("getMessage", {
// senderId,
// text,
// });
// });
// //when disconnect
// socket.on("disconnect", () => {
// console.log("a user disconnected!");
// removeUser(socket.id);
// io.emit("getUsers", users);
// });
// });
//////////////////////////////////////////
// END SOCKET IO
//////////////////////////////////////////
//////////////////////////////////////////
// HEROKU DEPLOY
//////////////////////////////////////////
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join("client/build")));
}
// app.get("*", (req, res) => {
// res.sendFile(path.join(__dirname, "client", "build", "index.html"));
// });
// } else {
// app.get("/", (req, res) => {
// res.send("API running");
// });
// }
// Development - set to listen on port 8800
// app.listen(8800, () => {
// console.log("Backend server is running!");
// });
// Production
// app.use('/api', router);
app.listen(PORT, console.log(`Server is running on port ${PORT}`));