-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
114 lines (99 loc) · 2.97 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
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
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const multer = require("multer");
const multerS3 = require("multer-s3");
const aws = require("aws-sdk");
const app = express();
const authRoute = require("./routes/auth");
const postRoute = require("./routes/postRoute");
const profileRoute = require("./routes/profileRoute");
const chatRoute = require("./routes/chatRoute");
const SocketManager = require("./SocketManager");
const keys = require("./config/keys");
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/images", express.static(path.join(__dirname, "images")));
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(null, Date.now() + "_" + file.originalname);
},
});
const s3 = new aws.S3({
accessKeyId: keys.AWS_ACCESS_KEY_ID,
secretAccessKey: keys.AWS_SECRET_ACCESS_KEY,
signatureVersion: "v4",
});
const s3Storage = multerS3({
s3: s3,
bucket: "socialconnectapp-bucket",
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
cb(null, Date.now() + "_" + file.originalname);
},
});
const filter = (req, file, cb) => {
if (
file.mimetype === "image/jpeg" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/png" ||
file.mimetype === "video/3gpp" ||
file.mimetype === "video/mp4" ||
file.mimetype === "video/mpeg" ||
file.mimetype === "video/webm" ||
file.mimetype === "video/quicktime"
) {
cb(null, true);
} else {
cb(null, false);
}
};
if (process.env.NODE_ENV == "production") {
app.use(
multer({ storage: s3Storage, fileFilter: filter }).array("fileImages")
);
}
else {
app.use(multer({ storage: fileStorage, fileFilter: filter }).array("images"));
}
// api
app.use(authRoute);
app.use(postRoute);
app.use(profileRoute);
app.use(chatRoute);
const port = process.env.PORT || 5000;
if (process.env.NODE_ENV == "production") {
app.use(express.static(path.join(__dirname, "client/build"))); // location of build folder
app.get("*", (req, res, next) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html")); // location of index.html file
});
}
mongoose
.connect(keys.MONGO_CONNECT_URL, {
useNewUrlParser: true,
})
.then((result) => {
console.log("mongodb connected");
const server = app.listen(port);
const io = require("./socket").init(server);
io.on("connection", SocketManager);
})
.catch((err) => {
console.log(err);
});