-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (55 loc) · 1.71 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
const express = require("express");
const app = express();
const http = require("http").Server(app);
// const io = require("socket.io")(http);
const ejs = require("ejs");
const glob = require("glob");
const path = require("path");
const cors = require("cors");
const { checkAuth } = require("./middlewares/auth");
const ResponseHandler = require("./utils/responseHandler");
const swaggerUI = require("swagger-ui-express");
const swaggerDoc = require("./swagger.json");
app.use(
'/api-docs',
swaggerUI.serve,
swaggerUI.setup(swaggerDoc)
);
// Block frames and others
// app.use(helmet());
// CORS
// app.use(cors());
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc
// app.set('trust proxy', 1);
// Template view engine
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.engine("ejs", ejs.renderFile);
// Forbidden endpoints
app.use(/.*\.(ejs|html)$/, function (req, res) {
res.status("404").json({ msg: "Page not found" });
});
// Incoming data parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Initialization
app.use(function (req, res, next) {
req.App = new ResponseHandler(req, res);
req.App.setToJSON();
next();
});
// Check and load authentication if exists
app.use(checkAuth);
// Auto require main controllers
glob.sync("./controllers/*.js").forEach(function (file) {
app.use(`/${path.parse(file)["name"]}`, require(path.resolve(file)));
});
// 404 error handler
app.use((req, res) => {
req.App.setPage("404").setMsg("Invalid request", "error").send();
});
// Server listening to port
const PORT = process.env.PORT || 9000;
http.listen(PORT, () => {
console.log("Server is running on port ", PORT);
});