-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (58 loc) · 1.82 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
require("dotenv").config();
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const swaggerUI = require("swagger-ui-express");
const YAML = require("yamljs");
const Joi = require("joi");
const cors = require("cors");
const helmet = require("helmet");
const compression = require("compression");
const xss = require("xss-clean");
const rateLimit = require("express-rate-limit");
const users = require("./routes/users");
const categories = require("./routes/categories");
const articles = require("./routes/articles");
const comments = require("./routes/comments");
const error = require("./middleware/error");
Joi.objectId = require("joi-objectid")(Joi);
const app = express();
app.use(express.json());
// 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: production packages
app.use(cors());
app.use(xss());
app.use(helmet());
app.use(compression());
// INFO: path for ststic file
app.use("/images", express.static(path.join(__dirname, "images")));
// INFO: Swagger
const swaggerDocument = YAML.load("./swagger.yaml");
app.get("/", (req, res) => {
res.send('<h1">Blog Api </h1><a href="/api-docs">Documentation</a>');
});
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerDocument));
app.use("/api/v1/articles", articles);
app.use("/api/v1/categories", categories);
app.use("/api/v1/comments", comments);
app.use("/api/v1/users", users);
app.use(error);
const MONGO_URL = process.env.MONGO_URL;
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} ...`))
);