-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
121 lines (98 loc) · 3.18 KB
/
app.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require("dotenv").config();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const express = require("express");
const favicon = require("serve-favicon");
const hbs = require("hbs");
const mongoose = require("mongoose");
const logger = require("morgan");
const path = require("path");
const cors = require("cors");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const flash = require("connect-flash");
mongoose
.connect(process.env.MONGODB_URI || "mongodb://localhost/motus", {
useNewUrlParser: true
})
.then(x => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch(err => {
console.error("Error connecting to mongo", err);
});
const app_name = require("./package.json").name;
const debug = require("debug")(
`${app_name}:${path.basename(__filename).split(".")[0]}`
);
const app = express();
// Middleware Setup
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Express View engine setup
app.use(
require("node-sass-middleware")({
src: path.join(__dirname, "public"),
dest: path.join(__dirname, "public"),
sourceMap: true
})
);
// app.set("views", path.join(__dirname, "views"));
// app.set("view engine", "hbs");
// app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "/client/build")));
app.use(favicon(path.join(__dirname, "public", "images", "favicon.ico")));
hbs.registerHelper("ifUndefined", (value, options) => {
if (arguments.length < 2)
throw new Error("Handlebars Helper ifUndefined needs 1 parameter");
if (typeof value !== undefined) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
// default value for title local
app.locals.title = "Express - Generated with IronGenerator";
// Enable authentication using session + passport
app.use(
session({
secret: "irongenerator",
resave: true,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection })
})
);
app.use(flash());
require("./passport")(app);
// allow access to the API from different domains/origins
app.use(
cors({
// this could be multiple domains/origins, but we will allow just our React app
origin: ["http://localhost:3000"]
})
);
const index = require("./routes/index");
app.use("/", index);
const authRoutes = require("./routes/auth");
app.use("/api/auth", authRoutes);
const contentManager = require("./routes/contentManager");
app.use("/api/content", contentManager);
const userRoutes = require("./routes/user");
app.use("/u", userRoutes);
const test = require("./routes/fileUpload");
app.use("/api", test);
const chart = require("./routes/chart");
app.use("/api/chart", chart);
const viewsRoutes = require("./routes/viewsRoutes");
app.use("/api/views", viewsRoutes);
const userSettingsRoutes = require("./routes/userSettings");
app.use("/api/userSettings", userSettingsRoutes);
app.use((req, res) => {
// If no routes match, send them the React HTML.
res.sendFile(__dirname + "/client/build/index.html");
});
module.exports = app;