-
Notifications
You must be signed in to change notification settings - Fork 57
/
server.js
52 lines (44 loc) · 1.41 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
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cors = require("cors");
const path = require("path");
const app = express();
const { authSocket, socketServer } = require("./socketServer");
const posts = require("./routes/posts");
const users = require("./routes/users");
const comments = require("./routes/comments");
const messages = require("./routes/messages");
const PostLike = require("./models/PostLike");
const Post = require("./models/Post");
dotenv.config();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer, {
cors: {
origin: ["http://localhost:3000", "https://post-it-heroku.herokuapp.com"],
},
});
io.use(authSocket);
io.on("connection", (socket) => socketServer(socket));
mongoose.connect(
process.env.MONGO_URI,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => {
console.log("MongoDB connected");
}
);
httpServer.listen(process.env.PORT || 4000, () => {
console.log("Listening");
});
app.use(express.json());
app.use(cors());
app.use("/api/posts", posts);
app.use("/api/users", users);
app.use("/api/comments", comments);
app.use("/api/messages", messages);
if (process.env.NODE_ENV == "production") {
app.use(express.static(path.join(__dirname, "/client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}