-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
80 lines (74 loc) · 2.07 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
/*
* _| _| _| _| _|_|_|
* _| _| _|_| _|_| _| _|
* _| _| _| _| _| _| _|
* _| _| _| _| _| _|
* _| _| _| _|_|_|
* EspressoJS - EspressoJS / Espresso
* Express Plug & Play Server
* -----------------
* @param {*} app - EspressoJS by Vimedev.com Labs
*/
require("dotenv").config();
const express = require("express");
const app = express();
const cfg = require("./server");
const {
readConfigFile,
setCustomCacheControl,
} = require("./server/utils/config.utils");
const configData = readConfigFile();
const Path = require("path");
const Cors = require("cors");
const Compression = require("compression");
const Favicon = require("serve-favicon");
const Static = require("serve-static");
const mongoose = require("mongoose");
const Routes = require("./routes/index");
const Port = configData.port || cfg.port;
const mongoConfig = configData.mongo;
const rootDir = process.cwd();
if (configData.mongo_isEnabled) {
const {
uri: mongoUri = configData.mongo.uri || "",
port: mongoPort = configData.mongo.port || "",
db: mongoDb = configData.mongo.db || "",
} = mongoConfig;
const hasPort = mongoPort ? `:${mongoPort}/` : "/";
const url = `mongodb+srv://${
process.env.MONGO_USER +
":" +
process.env.MONGO_TOKEN +
"@" +
mongoUri +
hasPort +
mongoDb
}`;
mongoose.Promise = global.Promise;
mongoose
.connect(url, {
useUnifiedTopology: true,
useNewUrlParser: true,
promiseLibrary: require("bluebird"),
})
.then(() => console.log(":: DB Connection successful ::"))
.catch((err) => console.error(err));
}
app.use(Compression());
app.use(Cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(Favicon(Path.join(rootDir, "public", "favicon.ico")));
app.use(
Static(Path.join(rootDir, "public"), {
maxAge: "1d",
setHeaders: setCustomCacheControl,
etag: true,
extensions: "error.html",
})
);
Routes(app);
app.listen(Port, () => {
console.log(`Server is running on port ${Port}`);
});
module.exports = app;