This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
81 lines (70 loc) · 2.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require("express");
var cors = require("cors");
const path = require("path");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const mongoose = require("mongoose");
const chalk = require("chalk"); // require chalk module to give colors to console text
require("dotenv").config();
const routes = require("./src/routes");
const models = require("./src/models");
const db = require("./src/models").db;
const app = express();
const connected = chalk.bold.cyan;
const error = chalk.bold.yellow;
const disconnected = chalk.bold.red;
const termination = chalk.bold.magenta;
if (process.env.NODE_ENV === "development") {
const DB_URI = process.env.MONGO_URI_TESTS;
app.use(cors());
} else {
const DB_URI = process.env.MONGO_URI_HACKMERCED;
}
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan("tiny"));
// All Api routes
app.use(
"/api",
routes.attendees,
routes.mailing,
routes.mentors,
routes.sponsors,
routes.volunteers
);
// Render React Application
app.use(express.static(path.join(__dirname, "/client/build/")));
// Connect to MongoDB then open port on defined port in .env
mongoose.connection.on("connected", function () {
console.log(
connected(
"Mongoose default connection is open to",
/(?=hack)(.*?)(?=\s*\?)/.exec(DB_URI)[0]
)
);
});
db().then(async () => {
mongoose.connection.on("error", function (err) {
console.log(
error("Mongoose default connection has occured " + err + " error")
);
});
mongoose.connection.on("disconnected", function () {
console.log(disconnected("Mongoose default connection is disconnected"));
});
process.on("SIGINT", function () {
mongoose.connection.close(function () {
console.log(
termination(
"Mongoose default connection is disconnected due to application termination"
)
);
process.exit(0);
});
});
app.listen(process.env.PORT || 3852, process.env.HOST || '0.0.0.0', () =>
console.log(
chalk.bold.white(`Hackm listening on port ${process.env.PORT}!`)
)
);
});