-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
84 lines (71 loc) · 2.35 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
81
82
83
84
const fs = require("fs");
const path = require("path");
const morgan = require("morgan");
const express = require("express");
const bulkify = require("bulkify");
const browserify = require("browserify");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const ngAnnotate = require("browserify-ngannotate");
global.rootRequire = function(name) {
return require(path.join(__dirname, name));
};
Object.values = function(obj) {
return Object.keys(obj).map(key => obj[key]);
};
const routes = rootRequire("server/routes");
const database = rootRequire("server/data-access/database");
const config = rootRequire("server/config.json");
if (process.argv[2] === "dev") {
config.dbIP = "mongodb://localhost:27017/test";
}
let initialize = true;
// Setting up the dist folder
if (!fs.existsSync(path.join(__dirname, "./app/dist/"))) {
fs.mkdirSync(path.join(__dirname, "./app/dist/"));
}
console.log("browserifying...");
const bundler = browserify(path.join(__dirname, "./app/js/app.js"));
bundler
.transform("babelify", {
presets: ["es2015"]
})
.transform(ngAnnotate, {})
.transform("brfs", {})
.transform(bulkify, {})
// .transform("uglifyify", { global: true })
.bundle()
.pipe(fs.createWriteStream(path.join(__dirname, "./app/dist/app.js")));
bundler
.pipeline
.get("wrap")
.on("end", () => {
// Setting up the server
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(morgan("dev"));
app.use(rootRequire("server/routes/middleware/addUserInfo"));
routes(app);
app.use(express.static(path.join(__dirname, "./app")));
app.use((req, res) => {
res.sendFile(path.join(__dirname, "./app/index.html"));
});
database.init(config)
.then(message => {
console.log(message);
if (!initialize) {
return;
}
const listener = app.listen(config, () => {
console.log("Listening on port: " + listener.address().port);
initialize = false;
});
})
.catch(error => {
console.error(error);
});
});