-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (54 loc) · 1.57 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 express = require("express");
const passport = require("passport");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const cors = require("cors");
const fileUpload = require("express-fileupload");
const { logger, httpLogger } = require("./src/loggers");
const { blogRouter, authRouter, authorRouter } = require("./src/routes");
const swaggerUi = require('swagger-ui-express');
const docs = require('./docs');
// Signup and login authentication middleware
require("./src/authentication/passport");
const app = express();
// Middleware
app.use(cors());
app.use(httpLogger);
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
standardHeaders: true,
legacyHeaders: true,
message: "Too many requests, please try again after 15 minutes",
});
app.use(limiter);
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(fileUpload({
createParentPath: true,
useTempFiles: true,
tempFileDir: "/tmp/",
}));
// Routes
app.use("/api/v1/blog", blogRouter);
app.use("/api/v1/auth", authRouter);
app.use(
"/api/v1/author/blog",
passport.authenticate("jwt", { session: false }),
authorRouter
);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(docs));
app.get("/", (req, res) => {
return res.json({ status: true });
});
// 404 route
app.use("*", (req, res) => {
return res.status(404).json({ message: "Route not found" });
});
// Error Handler
app.use(function (err, req, res, next) {
logger.error(err.message);
res.status(err.status || 500).send("Oops, something failed");
});
module.exports = app;