-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (54 loc) · 1.59 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
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const multer = require("multer");
const cors = require("cors");
const { v4: uuidv4 } = require("uuid");
const feedRoutes = require("./routes/feed");
const authRoutes = require("./routes/auth");
const KEYS = require("./keys");
const { appDir } = require("./util/file");
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
const name = uuidv4() + "." + file.originalname.split(".").reverse()[0];
cb(null, name);
},
});
const fileFilter = (req, file, cb) => {
const fileTypes = ["image/png", "image/jpg", "image/jpeg"];
if (fileTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(null, false);
}
};
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single("image")
);
app.use("/images", express.static(path.join(appDir, "images")));
app.use("/auth", authRoutes);
app.use("/feed", feedRoutes);
app.use((error, req, res, next) => {
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
return res.status(status).json({ message: message, data: data });
});
mongoose
.connect(KEYS.MONGODB_CONNECTION_URI)
.then(() => {
console.log("connected to the database");
app.listen(KEYS.PORT, () => {
console.log(`listening to port ${KEYS.PORT}`);
});
})
.catch((err) => {
console.log(err);
});