-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
67 lines (57 loc) · 1.71 KB
/
server.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
const express = require("express");
const logger = require("morgan");
const bodyParser = require("body-parser");
const cors = require("cors");
const compression = require("compression");
const urlPath = require("path");
const {
getHandlers,
getCoreHandlers,
getMiddlewares,
getRouteConfigs
} = require("./core");
const { PORT } = require("./config");
// Create app instance.
const app = express();
const middlewares = getMiddlewares();
const routeConfigs = getRouteConfigs();
const handlers = getHandlers();
const coreHandlers = getCoreHandlers();
// Initializing default middlewares
app.use(cors());
app.use(compression());
app.use(logger("dev"));
app.use(middlewares.config);
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
// TODO: Optimize below code block for readablity and scalability.
const methods = Object.keys(routeConfigs);
methods.forEach(methodKey => {
const method = routeConfigs[methodKey];
method.forEach(config => {
const { path, type, func } = config;
const funcHandler = methodKey.toLowerCase();
if (!type || type === "default") {
const configMiddlewares = config.middlewares || [];
const middlewareArray = configMiddlewares.map(m => middlewares[m]);
app[funcHandler](path, middlewareArray, handlers[funcHandler][func]);
}
if (type === "proxy") {
const { endpoint } = config;
app[funcHandler](
path,
coreHandlers[funcHandler].proxy.bind(null, config)
);
}
if (type === "static") {
const { entrypoint } = config;
app.use(path, express.static(urlPath.join(__dirname, entrypoint)));
}
});
});
// Server
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));