-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (97 loc) · 2.72 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
const path = require("path");
const winston = require("winston");
const swaggerUI = require("swagger-ui-express");
const YAML = require("yamljs");
const helmet = require("helmet");
const cors = require("cors");
const xss = require("xss-clean");
const rateLimit = require("express-rate-limit");
const compression = require("compression");
const multer = require("multer");
const express = require("express");
const mongoose = require("mongoose");
const Joi = require("joi");
const dotenv = require("dotenv");
const error = require("./middleware/error");
const projects = require("./routes/projects");
const Bugs = require("./routes/bugs");
const users = require("./routes/users");
const auth = require("./routes/auth");
const app = express();
Joi.objectId = require("joi-objectid")(Joi);
app.use(express.json());
dotenv.config();
// INFO: if we behind a proxy
app.set("trust proxy", 1);
// INFO: use it to limit number of reqest
app.use(
rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
})
);
// INFO: Swagger
const swaggerDocument = YAML.load("./swagger.yaml");
// INFO: production packages
app.use(cors());
app.use(xss());
app.use(helmet());
app.use(compression());
// INFO: Setup image folder and image name OR image URL
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(
null,
new Date().toISOString().replace(/[\/\\:]/g, "-") +
"-" +
file.originalname
);
},
});
// INFO: Type of file that acceptaple to upload
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg" ||
file.mimetype === "image/PNG" ||
file.mimetype === "image/JPG" ||
file.mimetype === "image/JPEG"
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single(
"profileImage"
)
);
app.use("/images", express.static(path.join(__dirname, "images")));
// INFO: logging error middleware
require("./middleware/log")();
// INFO: api routes
app.get("/", (req, res) => {
res.send('<h1>Bugs Tracker Api<h1><a href="/api-docs">Documentation</a>');
});
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerDocument));
app.use("/api/v1/users", users);
app.use("/api/v1/auth", auth);
app.use("/api/v1/projects", projects);
app.use("/api/v1/bugs", Bugs);
app.use(error);
const MONGO_URL = process.env.MONGO_URI;
const PORT = process.env.PORT || 5000;
mongoose
.connect(MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() =>
app.listen(PORT, () => console.log(`server running on port ${PORT} ...`))
);