-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
55 lines (49 loc) · 1.69 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
53
54
55
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const helmet = require("helmet");
const auth = require("./routes/auth");
const admin = require("./routes/admin");
const video = require("./routes/video");
const user = require("./routes/user");
const chat = require("./routes/chat");
const errorHandler = require("./middleware/errorhandle");
const DB = require("./utils/db.config");
const User = require("./models/User");
const { startSocket } = require("./utils/socketconnection");
const app = express();
app.use(express.json());
app.use(cors());
app.use(helmet.frameguard({ action: 'DENY' }));
app.use(function (req, res, next) {
if (process.env.NODE_ENV === "production"){
res.setHeader('Access-Control-Allow-Origin', process.env.SERVER_URL);
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'), { lastModified: false });
})
}
if ((req.get('X-Forwarded-Proto') !== 'https' && process.env.NODE_ENV == "production")) {
res.redirect('https://' + req.get('Host') + req.url);
} else
next();
});
DB();
const clearSubs = async ()=>{
const users = await User.find({});
for(x of users){
x.subscribedto = [];
x.subscribers = [];
await x.save();
}
}
app.use("/api/v1/auth", auth);
app.use("/api/v1/admin", admin);
app.use("/api/v1/chat",chat);
app.use("/api/v1/video", video);
app.use("/api/v1/user", user);
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, function(){console.log(`Server started at http://localhost:${PORT}`)});
startSocket(server);